@ "Comments"配列が0で、他の2つが1以上の場合、次のエラーが発生します。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
これはself.itemsアレイにデータを入力しています。
self.notification一部の(この場合はコメント)配列がnullであり、このタイプのエラーをスローせずにコードを実行できるという事実をどのように説明できますか?
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];
}