iphone - 理解语法的 NSFetchedResultsSectionInfo

标签 iphone objective-c core-data

我正在学习有关 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/

相关文章:

ios - Open GL ES,多个着色器

objective-c - 安排生成敌人的方法或使用敌人缓存的更新方法是否更有效?

ios - 将可转换的 NSAttributedString 保存到核心数据时发生极其奇怪的崩溃

iphone - 在 iTunes Connect 中将应用程序类型从 iPhone 更改为通用

iphone - PhoneGap 和 Zxing QrCode - 构建问题

iphone - 为 iPad 编写自己的 RSS 阅读器之前的问题

iphone - 如何将 userInfo 添加到 UIAlertView?

ios - UIWebView 将 Objective C 暴露给 JavaScript

ios - 编辑核心数据会产生错误,此类与键的键值编码不兼容

ios - 创建复杂核心数据数据库的技巧