2

我对 OBJ-C 中的协议有一些疑问。我经常看到用协议声明 id 对象的行,例如:

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

    id  <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
    NSLog(@"%i",[secInfo numberOfObjects]);
    return [secInfo numberOfObjects];
}

我根本不明白以下行:

id  <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
  1. “身份证”是什么意思?
  2. “objectAtIndex:section”返回什么对象?
  3. 为什么我可以在对象上调用“[secInfo numberOfObjects]”?我的意思是,方法 numberOfObjects 是如何存在的?

我正在尝试精简 CoreData,但如果不理解这一点,我实际上无法继续。谢谢你。

4

5 回答 5

3

是什么id <ProtocolName>意思?

它的意思是“指向类实现ProtocolName协议的任何 Objective-C 对象的指针”。

返回什么对象objectAtIndex:section

它返回符合<NSFetchedResultsSectionInfo>协议的任何类的对象。

为什么我可以调用[secInfo numberOfObjects]对象?

为什么不?

我的意思是,该方法如何numberOfObjects存在?

返回对象的类实现它。

于 2013-01-07T17:38:56.693 回答
2

如果您有 Java 背景,可以将其NSFetchedResultsSectionInfo视为 Java 接口而不是具体类,函数内容如下所示:

NSFetchedResultsSectionInfo secInfo = this.fetchedResultsController.sections().objectAtIndex(section); 
System.out.println(secInfo.numberOfObjects());
return secInfo.numberOfObjects();

numberOfObjects可以调用,id <NSFetchedResultsSectionInfo> secInfo因为这是在 Objective-C 中声明此类事物的等效方式——它不是一个特定的类,而是一个“接口”。

于 2013-01-07T17:46:33.487 回答
1
  1. id is the generic type for a pointer to any Objective-C object. Declaring a variable of type id means it can reference any object; declaring a variable of type id <SomeProtocol> means it can reference an object of any class, but it must conform to SomeProtocol.

  2. [self.fetchedResultsController sections] returns an array, in which each element is an object (whose class is unknown to you) implementing the NSFetchedResultsSectionInfo protocol and representing a section in your table. The method you're implementing wants to know the number of rows in the section whose index is passed in as the section parameter, so you call objectAtIndex:section on that array to get the id <NSFetchedResultsSectionInfo> object representing that section.

  3. You can call [secInfo numberOfObjects] because you've declared secInfo to reference an object which implements the NSFetchedResultsSectionInfo protocol; numberOfObjects is a method in that protocol. (And it actually works because, at runtime, it really is an object implementing that protocol, as expected from the documented behavior of -[NSFetchedResultsController sections].)

I'm trying to lean CoreData but I can't actually continue without understanding this.

Protocols aren't just a Core Data thing; if you're having trouble with them I recommend reading up on the Objective-C language documentation.

于 2013-01-07T17:49:21.177 回答
1
  1. id意思是“任何类型的对象”。id <NSFetchedResultsSectionInfo>意思是“任何类型的对象,只要它实现了NSFetchedResultsSectionInfo协议。协议只是方法签名的列表,如果一个对象实现了协议,这意味着它必须实现该协议的所有必需方法。

  2. 你真的不在乎。它可以是实现NSFetchedResultsSectionInfo协议的任何对象,但您并不真正关心它是什么类型的对象。您只需要知道您可以从NSFetchedResultsSectionInfo协议中向它发送任何所需的消息。

  3. secInfo是实现NSFetchedResultsSectionInfo协议的对象。如果您查看协议(cmd + 单击该协议将带您进入其定义),您会看到列表中有一条名为numberOfObjects. 这意味着可以保证任何实现此协议的对象都将实现numberOfObjects.

于 2013-01-07T17:45:02.367 回答
0

What does " id " mean?

id<NSFetchedResultsSectionInfo> is used to guarantee that the object conforms to the NSFetchedResultsSectionInfo, so that if it's not, you get a compiler warning. It is useful for this.

What object does "objectAtIndex:section" return?

id is the root of all pointer to objects.

Usually id is used when there isn't the certainty that the returned object will be of a type. The only thing known is that it conforms to the NSFetchedResultsSectionInfo protocol. So that's the case: you don't know which type of object will be returned, just that it conforms to that protocol.

Why can I call "[secInfo numberOfObjects]" on the object? I mean, How's the method numberOfObjects exists?

Just see the documentation, it's implemented by the class.

于 2013-01-07T17:50:50.353 回答