理解语法的NSFetchedResultsSectionInfo

时间:2014-01-07 11:25:54

标签: iphone objective-c core-data

我正在学习有关核心数据的教程,我对以下代码行感到困惑:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}

我知道NSFetchedResultSectionInfo是协议,但协议是在类头中定义的方法列表。我不明白这里发生了什么,双方括号[..][..];的含义是什么?

也许这是Objective-c的新现代语法,我不知道,如果有人能为我解释清楚,那就太好了,谢谢。

2 个答案:

答案 0 :(得分:3)

在这种情况下,

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]

是一个类,它确认了协议NSFetchedResultsSectionInfo

因此编译器将调用[sectionInfo numberOfObjects];解释为有效;)

答案 1 :(得分:1)

<NSFetchedResultsSectionInfo>表示此处NSFetchedResultsSectionInfo个对象的数组。

因此,每个括号都等于NSFetchedResultsSectionInfo个对象。