3.15.2010

Objective C - Categories

If you follow my Twitter account you may have seen a recent posting where I gripe about Objective C. I've recently started working on an iPhone application (re: game) and am therefore learning Objective C in the process. My immediate impressions with Objective C aren't good but I'll save that for a much longer post to come later. One thing I would like to mention now, though, Objective C calls "Categories." You may already have experience with categories if you've done much JavaScript programming (where you modify an object's prototype), C#'s extension methods, or re-opening a class in Ruby. I had worked with object prototypes in JavaScript before but I guess I never really caught on to just what was happening.

Categories are a way of extending existing class implementations without having to create a whole new class that inherits from the original. This is such an amazingly powerful feature! It really opens up the door to amazing possibilities. I should warn you, however, that I am extremely new to this methodology and therefore I am sure there are all sorts of things that I have yet to realize about them.

I wrote up about half an example on how to do this and decided it's not worth it. There are plenty of other really good examples already out there. Try here. He has a really good example adding a shuffle method to the NSMutableArray. Have you ever been using a class from a library and said, "Wow, this is great but I really need it to do X?" Well with this you can add your own implementation to the class and use it freely throughout your code.

0 comments:

 
Copyright 2009 BraiNuggets