Tuesday March 20, 2012

Collections and Enumerations in Objective-C

I had the pleasure of meeting Heath Borders at the Chicaco CocoaConf. He pointed out his HBCollections iOS library and I just had to mention it here.

He originally wrote it as a way of easily encapsulating operations on collections, like you would in Lisp. Mapping, reducing, filtering are all fundamental operations over there and they are slowly making their way to Objective-C with things like enumerateObjectsUsingBlock:.

The best part is the way Heath handles the idea of breaking out of an enumeration. Those who use enumerateObjectsUsingBlock: and the like will recall that the only way to break is by assigning a value to the stop parameter that must be accepted by the block you write. It’s a fundamental limitation from the way blocks are implemented in C. Heath built his solution so you can pass in another block that decides if enumeration should continue or not. It’s pretty clever.