有人知道如何实现NSFastEnumeration协议吗?

时间:2021-11-15 02:47:38

I have a class and I want my class to confirm to the NSFastEnumeration Protocol. I've read the documentation but it's not really clear. Can someone please tell me what the protocol method should return and how it works? Thanks in advance.

我有一个类,我想要我的类确认到NSFastEnumeration协议。我看过文档,但还不清楚。谁能告诉我应该返回什么协议方法以及它是如何工作的?提前谢谢。

2 个解决方案

#1


41  

Apple's FastEnumerationSample shows you what to do, but here's a breakdown.

苹果的FastEnumerationSample向您展示了该怎么做,但这里有一个细分。

The sole NSFastEnumeration method, countByEnumeratingWithState:objects:count:, returns chunks of the collection. It's executed whenever more items are needed, until it indicates that there are no more items by returning 0. A chunk is passed as a C array of ids.

唯一的NSFastEnumeration方法countByEnumeratingWithState:objects:count:返回集合的块。每当需要更多项时,它就执行,直到返回0表示不再有项为止。块作为id的C数组传递。

Within the method, the state parameter holds most (if not all) of the data you'll be using. You'll need to set state->itemsPtr and update state->state with each separate invocation of countByEnumeratingWithState:objects:count:. Here's a brief description of each field of NSFastEnumerationState:

在该方法中,状态参数保存您将要使用的大部分数据(如果不是全部的话)。您需要为countByEnumeratingWithState:objects:count:设置状态->itemsPtr和更新状态->状态。以下是NSFastEnumerationState每个字段的简要描述:

  • state: represents the position in the sequence being iterated over. For indexed collections, this would be the index. For linked lists, this could be a node pointer. For other types, this could be a more complex type (e.g. for a tree, state->state could be an NSMutableArray used as a stack to store nodes). When countByEnumeratingWithState:objects:count: is first called, state->state is 0; check for this condition to initialize the state struct.
  • 状态:表示迭代序列中的位置。对于索引的集合,这将是索引。对于链表,这可以是一个节点指针。对于其他类型,这可能是一种更复杂的类型(例如,对于树,状态->状态可以是用于存储节点的栈的NSMutableArray)。当countByEnumeratingWithState:objects:count:首先被调用,state->state为0;检查此条件以初始化状态结构。
  • itemsPtr: the items in the chunk; points to a C array of ids. Cocoa will loop over this array, binding each item in turn to the variable named in the for-in loop.
  • itemsPtr:块中的项目;指向一个C数组的id。Cocoa将对这个数组进行循环,将每个条目依次绑定到forin循环中命名的变量。
  • mutationsPtr: for mutable collections, used to indicate that the collection has changed since the last call to countByEnumeratingWithState:objects:count:. Typically, you'd set this once when initializing the state. Collection mutators increment the value that this points to. Cocoa will compare the value returned by countByEnumeratingWithState:objects:count: to the value from the previous invocation; if they're different, Cocoa will throw an exception.
  • mutationsPtr:对于可变集合,用于指示自上次调用countByEnumeratingWithState:objects:count:之后集合发生了更改。通常,在初始化状态时设置一次。集合变址器增加此值指向的值。Cocoa将会比较countByEnumeratingWithState:objects:count:返回的值与之前调用的值;如果它们不一样,Cocoa将抛出一个异常。
  • extra: you can use this to store extra data.
  • 额外:您可以使用它来存储额外的数据。

You can set state->state and any element of state->extra to whatever you wish; they're provided solely for your convenience, and do not affect Cocoa. state->itemsPtr, *state->mutationsPtr and the value returned by the method, however, do affect Cocoa.

你可以设置状态->状态和状态的任何元素->,不管你想要什么;它们仅供您方便使用,不影响可可。然而,状态->itemsPtr, *state->mutationsPtr以及方法返回的值确实会影响Cocoa。

As for the two other method parameters, stackbuf is an array that Cocoa provides to hold items. Its use is optional, but if you don't use it, you'll have to allocate storage space for state->itemPtr. If you use it, set state->itemsPtr to stackbuf with each invocation. len is the length of stackbuf, the maximum number of items that you'll be able to store in it.

至于其他两个方法参数,stackbuf是Cocoa提供的一个数组。它的使用是可选的,但是如果您不使用它,您将不得不为state->itemPtr分配存储空间。如果使用它,则在每次调用时将state->itemsPtr设置为stackbuf。len是stackbuf的长度,您可以在其中存储的项目的最大数量。

Further reading:

进一步阅读:

#2


3  

Just reviving this thread after finding an excellent explanation. The Apple link seems to be broken. You can try here: https://developer.apple.com/library/ios/#samplecode/FastEnumerationSample/Introduction/Intro.html

在找到一个很好的解释之后,重新启动这个线程。苹果与苹果的联系似乎被打破了。您可以在这里尝试:https://developer.apple.com/library/ios/#samplecode/FastEnumerationSample/Introduction/Intro.html

The best example for implementing fast enumeration that I've found is at: http://mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html. It looks much worse than it is.

实现快速枚举的最佳示例是:http://mikeash.com/pyblog/friday-qa-2010-04-16实现快速枚举。它看起来比实际更糟。

#1


41  

Apple's FastEnumerationSample shows you what to do, but here's a breakdown.

苹果的FastEnumerationSample向您展示了该怎么做,但这里有一个细分。

The sole NSFastEnumeration method, countByEnumeratingWithState:objects:count:, returns chunks of the collection. It's executed whenever more items are needed, until it indicates that there are no more items by returning 0. A chunk is passed as a C array of ids.

唯一的NSFastEnumeration方法countByEnumeratingWithState:objects:count:返回集合的块。每当需要更多项时,它就执行,直到返回0表示不再有项为止。块作为id的C数组传递。

Within the method, the state parameter holds most (if not all) of the data you'll be using. You'll need to set state->itemsPtr and update state->state with each separate invocation of countByEnumeratingWithState:objects:count:. Here's a brief description of each field of NSFastEnumerationState:

在该方法中,状态参数保存您将要使用的大部分数据(如果不是全部的话)。您需要为countByEnumeratingWithState:objects:count:设置状态->itemsPtr和更新状态->状态。以下是NSFastEnumerationState每个字段的简要描述:

  • state: represents the position in the sequence being iterated over. For indexed collections, this would be the index. For linked lists, this could be a node pointer. For other types, this could be a more complex type (e.g. for a tree, state->state could be an NSMutableArray used as a stack to store nodes). When countByEnumeratingWithState:objects:count: is first called, state->state is 0; check for this condition to initialize the state struct.
  • 状态:表示迭代序列中的位置。对于索引的集合,这将是索引。对于链表,这可以是一个节点指针。对于其他类型,这可能是一种更复杂的类型(例如,对于树,状态->状态可以是用于存储节点的栈的NSMutableArray)。当countByEnumeratingWithState:objects:count:首先被调用,state->state为0;检查此条件以初始化状态结构。
  • itemsPtr: the items in the chunk; points to a C array of ids. Cocoa will loop over this array, binding each item in turn to the variable named in the for-in loop.
  • itemsPtr:块中的项目;指向一个C数组的id。Cocoa将对这个数组进行循环,将每个条目依次绑定到forin循环中命名的变量。
  • mutationsPtr: for mutable collections, used to indicate that the collection has changed since the last call to countByEnumeratingWithState:objects:count:. Typically, you'd set this once when initializing the state. Collection mutators increment the value that this points to. Cocoa will compare the value returned by countByEnumeratingWithState:objects:count: to the value from the previous invocation; if they're different, Cocoa will throw an exception.
  • mutationsPtr:对于可变集合,用于指示自上次调用countByEnumeratingWithState:objects:count:之后集合发生了更改。通常,在初始化状态时设置一次。集合变址器增加此值指向的值。Cocoa将会比较countByEnumeratingWithState:objects:count:返回的值与之前调用的值;如果它们不一样,Cocoa将抛出一个异常。
  • extra: you can use this to store extra data.
  • 额外:您可以使用它来存储额外的数据。

You can set state->state and any element of state->extra to whatever you wish; they're provided solely for your convenience, and do not affect Cocoa. state->itemsPtr, *state->mutationsPtr and the value returned by the method, however, do affect Cocoa.

你可以设置状态->状态和状态的任何元素->,不管你想要什么;它们仅供您方便使用,不影响可可。然而,状态->itemsPtr, *state->mutationsPtr以及方法返回的值确实会影响Cocoa。

As for the two other method parameters, stackbuf is an array that Cocoa provides to hold items. Its use is optional, but if you don't use it, you'll have to allocate storage space for state->itemPtr. If you use it, set state->itemsPtr to stackbuf with each invocation. len is the length of stackbuf, the maximum number of items that you'll be able to store in it.

至于其他两个方法参数,stackbuf是Cocoa提供的一个数组。它的使用是可选的,但是如果您不使用它,您将不得不为state->itemPtr分配存储空间。如果使用它,则在每次调用时将state->itemsPtr设置为stackbuf。len是stackbuf的长度,您可以在其中存储的项目的最大数量。

Further reading:

进一步阅读:

#2


3  

Just reviving this thread after finding an excellent explanation. The Apple link seems to be broken. You can try here: https://developer.apple.com/library/ios/#samplecode/FastEnumerationSample/Introduction/Intro.html

在找到一个很好的解释之后,重新启动这个线程。苹果与苹果的联系似乎被打破了。您可以在这里尝试:https://developer.apple.com/library/ios/#samplecode/FastEnumerationSample/Introduction/Intro.html

The best example for implementing fast enumeration that I've found is at: http://mikeash.com/pyblog/friday-qa-2010-04-16-implementing-fast-enumeration.html. It looks much worse than it is.

实现快速枚举的最佳示例是:http://mikeash.com/pyblog/friday-qa-2010-04-16实现快速枚举。它看起来比实际更糟。