在 Objective-C 2.0 中提供了快速枚举的语法,它是我们遍历集合元素的首选方法,因为它具有以下优点:比直接使用NSEnumerator更高效;语法非常简洁;如果集合在遍历的过程中被修改,它会抛出异常;可以同时执行多个枚举。那么问题来了,它是如何做到的呢?我想,你应该也跟我一样,对 Objective-C 中快速枚举的实现原理非常感兴趣,事不宜迟,让我们来一探究竟吧。解析 NSFastEnumeration 协议在 Objective-C 中,我们要想实现快速枚举就必须要实现NSFastEnumeration协议,在这个协议中,只声明了一个必须实现的方法:1234567891011121314/**Returns by reference a C array of objects over which the sender should iterate, and as the return value the number of objects in the array.@param state Context information that is used in the enumeration to, in addition to other possibilities, ensure that the collection has not been mutated
...
继续阅读
(21)