iPhone - 数组计数的numberOfRowsInSection错误
如果@“Comments”数组为0且其他两个为1或更大,则会出现以下错误。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
这填充了self.items
数组。
我如何解释这样一个事实:某些self.notification
(在这种情况下为注释)数组可能为空并仍然运行我的代码而不会抛出此类错误?
NSDictionary *commentsDict = [NSDictionary dictionaryWithObject:self.notification.Comments forKey:@"Comments"];
NSDictionary *likesDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Likes"];
NSDictionary *friendsDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Friends"];
self.items = [NSMutableArray array];
[self.items addObject:commentsDict];
[self.items addObject:likesDict];
[self.items addObject:friendsDict];
这是它失败的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [self.items objectAtIndex:section];
NSArray *array = nil;
if (section == 0) {
array = [dictionary objectForKey:@"Comments"]; // 0 count
} else if (section == 1) {
array = [dictionary objectForKey:@"Likes"]; // 1 count or greater
} else {
array = [dictionary objectForKey:@"Friends"]; // 1 count or greater
}
return [array count];
}
没有找到相关结果
已邀请:
5 个回复
痰降锭骂奸
,你会发现数组确实是空的。
犀寺扦
数组没有每个部分索引的对象。从错误消息看起来
实际上是空的(零对象)。
吠强祷豪硅
盛梯獭八搽
以及如何实现
。
要么提前释放数组,要么返回的部分数量与
无关。
脾萌
的问题,因为在更改节标题时我只引用了Comments数组而不是其他两个。