我这里有这段代码:
for (int i = 0; i<[taskData count]; i++)
{
for (int j=0; j<[allJobTaskArray count]; j++)
{
NSLog(@"%d", i);
NSLog(@"%d", j);
PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
pldCell.cellSelected = NO;
[punchListData addObject:pldCell];
}
}
现在让我解释一下:
- taskData 计数为 57,是一个 NSArray
- allJobTaskArray 计数为 12,是一个 NSMutableArray
- 这段代码会在这一行崩溃:
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]]; when j 是 6 而 i 是 36 简单,因为在 allJobTaskArray objectAtIndex: 6 objectAtIndex: 36 不存在。
- 这是我得到的错误:
[__NSCFArray objectAtIndex:]: index (36) beyond bounds (36)
- 我想要做的是如果项目不存在,那么
pldCell 应该等于 @"" ;
我尝试了以下方法:
if([[allJobTaskArray objectAtIndex:j] objectAtIndex:i] == [NSNull null]){
pldCell.stringData = @"";
}else{
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
}
Best Answer-推荐答案 strong>
总而言之,它应该看起来像 -
for (int i = 0; i<[taskData count]; i++)
{
for (int j=0; j<[allJobTaskArray count]; j++)
{
NSLog(@"%d", i);
NSLog(@"%d", j);
PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
if ([[allJobTaskArray objectAtIndex:j] count] > i) {
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
} else {
pldCell.stringData = @"";
}
pldCell.cellSelected = NO;
[punchListData addObject:pldCell];
}
}
关于ios - Objective-C 检查 NSMutableArray 中是否存在项目,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/30316157/
|