NSEnumerator / NSFastEnumeration
Enumeration is where computation gets interesting. It’s one thing to encode logic that’s executed once, but applying it across a collection, that’s what makes programming so powerful.
Enumeration makes computation powerful by applying logic to collections.
Objective-C bridges C's procedural style and Smalltalk's object-oriented model.
Ways to enumerate:
C loops use for or while with an index, but they risk off-by-one errors.
List comprehension with for/in uses NSFastEnumeration for speed and safety. It works on classes like NSArray, NSSet, NSDictionary.
NSEnumerator gives nextObject and allObjects for forward iteration. It helps add enumeration to custom objects.
Block enumeration with enumerateObjectsUsingBlock: gives index and stop options. It has concurrent or reverse modes, but fast enumeration is usually quicker.
Higher abstractions like for/in are easier, less error-prone, and often faster than low-level loops.
Enjoyed this post?
Well, you could share the post with others, follow me with RSS Feeds, send me a comment via email, and/or leave a donation in the Tip Jar.
Tags
Category:
Tags:
Year: