问题描述
我有一个课,我希望我的课向NSFastEnumeration协议确认.我已经阅读了文档,但不清楚.有人可以告诉我协议方法应该返回什么以及如何工作吗?
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?
推荐答案
Apple的 FastEnumerationSample 向您显示了要执行的操作,但这是一个细分.
Apple's FastEnumerationSample shows you what to do, but here's a breakdown.
唯一的NSFastEnumeration
方法countByEnumeratingWithState:objects:count:
返回集合的大块.每当需要更多项目时便会执行该操作,直到通过返回0指示没有更多项目为止.将块作为id
s的C数组传递.
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 id
s.
在该方法中,state
参数保存您将使用的大多数(如果不是全部)数据.您需要设置state->itemsPtr
并用countByEnumeratingWithState:objects:count:
的每个单独调用更新state->state
.以下是NSFastEnumerationState
每个字段的简要说明:
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
:
-
state
:表示要迭代的序列中的位置.对于索引集合,这将是索引.对于链表,这可以是节点指针.对于其他类型,这可能是更复杂的类型(例如,对于树,state->state
可能是NSMutableArray,用作堆栈来存储节点).首次调用countByEnumeratingWithState:objects:count:
时,state->state
为0;否则为0.检查此条件以初始化state
结构. -
itemsPtr
:块中的项目;指向id
s的C数组.可可将遍历此数组,依次将每个项目绑定到for-in循环中命名的变量. -
mutationsPtr
:用于可变集合,用于指示自上次调用countByEnumeratingWithState:objects:count:
以来该集合已更改.通常,初始化状态时只需设置一次.集合更改器将其指向的值增加.可可会将countByEnumeratingWithState:objects:count:
返回的值与上一次调用的值进行比较;如果它们不同,可可将引发异常. -
extra
:您可以使用它来存储额外的数据.
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). WhencountByEnumeratingWithState:objects:count:
is first called,state->state
is 0; check for this condition to initialize thestate
struct.itemsPtr
: the items in the chunk; points to a C array ofid
s. Cocoa will loop over this array, binding each item in turn to the variable named in the for-in loop.mutationsPtr
: for mutable collections, used to indicate that the collection has changed since the last call tocountByEnumeratingWithState: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 bycountByEnumeratingWithState:objects:count:
to the value from the previous invocation; if they're different, Cocoa will throw an exception.extra
: you can use this to store extra data.
您可以将state->state
和state->extra
的任何元素设置为所需的任何内容.提供它们仅仅是为了您的方便,并不影响可可. state->itemsPtr
,*state->mutationsPtr
和方法返回的值确实会影响可可粉.
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.
对于其他两个方法参数,stackbuf
是Cocoa提供的用于保存项目的数组.它的使用是可选的,但是如果您不使用它,则必须为state->itemPtr
分配存储空间.如果使用它,请在每次调用时将state->itemsPtr
设置为stackbuf
. len
是stackbuf
的长度,它是您可以在其中存储的最大项目数.
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.
进一步阅读:
- 2010年星期五问答集-04-16:实现快速枚举(mikeash.com)
- 实施countByEnumeratingWithState:objects:count:(充满可可的爱)
- NSFastEnumeration协议参考
- 在自定义类上实现NSFastEnumerator (SO)
- Friday Q&A 2010-04-16: Implementing Fast Enumeration (mikeash.com)
- Implementing countByEnumeratingWithState:objects:count: (Cocoa with Love)
- NSFastEnumeration Protocol Reference
- Implementing NSFastEnumerator on Custom Class (SO)
这篇关于如何实现NSFastEnumeration协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!