Friday, November 23, 2012

Objective-C Categories to Extend Collections

My first use for Objective-C categories was to extend the NSArray class to act like a Stack and then later, like a Queue.

Here is a post about how to use categories:
http://macdevelopertips.com/objective-c/objective-c-categories.html

Here is a post about how to use them to make Stacks and Queues:
http://stackoverflow.com/questions/817469/how-do-i-make-and-use-a-queue-in-objective-c

One thing that initially threw me for a loop is the naming. They do expect you to use a '+' in the name and that is OK in Mac OS X and XCode.

Another thing that threw me for a loop is that unlike a Generic or a sub-class, they're simply injected (available) for every instance of the object to whom the category belongs.

That is to say, you do not need to create a Stack -- if you have the category, all your NSArray can now act like a Stack if you send that message to the instance.


No comments: