I have some questions about protocols in OBJ-C.
I often see lines that declare an id object with a protocol, for example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
NSLog(@"%i",[secInfo numberOfObjects]);
return [secInfo numberOfObjects];
}
I don’t understand the following line at all:
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
- What does ” id ” mean?
- What object does “objectAtIndex:section” return?
- Why can I call “[secInfo numberOfObjects]” on the object?
I mean, How’s the method numberOfObjects exists?
I’m trying to lean CoreData but I can’t actually continue without understanding this.
Thank you.
It means “a pointer to any Objective-C object of which the class implements the
ProtocolNameprotocol”.It returns objects of any class, which conform to the
<NSFetchedResultsSectionInfo>protocol.Why not?
The class of the returned object implements it.