我正在学习有关 Core Data 的教程,我对以下代码行感到困惑:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
据我所知,NSFetchedResultSectionInfo 是协议(protocol),但协议(protocol)是类 header 中定义的方法列表。我不明白这里发生了什么,双方括号 [..][..];
是什么意思?
也许这是我不知道的 objective-c 的新现代语法,如果有人能为我解释得更清楚,那就太好了,谢谢。
最佳答案
id <NSFetchedResultsSectionInfo> sectionInfo
在这种情况下,意味着
[self.fetchedResultsController sections][section];
// ->
NSArray *sections = [self.fetchedResultsController sections]; // array of objects, that confirm <NSFetchedResultsSectionInfo>
// sections[section] is same as [sections objectAtIndex:section]
是一个类,确认协议(protocol) NSFetchedResultsSectionInfo
所以调用[sectionInfo numberOfObjects];
将被编译器解释为有效 ;)
关于iphone - 理解语法的 NSFetchedResultsSectionInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20970396/