iam*_*toc 7 iphone objective-c nsarray
if (![[array objectAtIndex:indexPath.row] isEmpty]) {
.... proceed as necessary
}
Run Code Online (Sandbox Code Playgroud)
indexPath.row可以包含任何类型的对象,也可以为空.通常它是空的,因此当它在null时,在尝试检索指定位置处的对象时会发生阻塞.我已经尝试过上述方法,但这也不起作用.检查此方案的正确方法是什么?
Emp*_*ack 20
如果objectAtIndex:不知道数组是否包含索引处的对象,则不应该调用.相反,你应该检查,
if (indexPath.row < [array count])
Run Code Online (Sandbox Code Playgroud)
如果您使用arraytableView作为数据源.你应该简单地返回[array count]行数,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
Run Code Online (Sandbox Code Playgroud)
并且,只需在indexPath.row中获取对象,而无需检查任何条件.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Other code
NSObject *obj = [array objectAtIndex:indexPath.row];
// Proceed with obj
}
Run Code Online (Sandbox Code Playgroud)
小智 6
使用[array count]方法:
if (indexPath.row < [array count])
{
//The element Exists, you can write your code here
}
else
{
//No element exists at this index, you will receive index out of bounds exception and your application will crash if you ask for object at current indexPath.row.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9071 次 |
| 最近记录: |