When writing our own program, you’ll often want to add some new method to an existing class. You can always add a subclass to an existing class if you want to add some more method to it. But there are limitations to that, not every time subclassing is convenient enough for people to subclass. For example, you want to add an email address validator method to NSString, but NSString is the top of the hierarchy of the class cluster, which makes it difficult to subclass.
Note: A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way provides a simplified interface to the user, who sees only the publicly visible architecture. Read more about class clusters here: http://developer.apple.com/library/mac/#documentation/General/
But because of the difficulties, Objective-C has this brilliant mechanism that will let you add a new method to an existing class without any difficulties. The term for is called Categories.
Creating a Category Method
A category is a way to add a new method to an existing class even if you don’t have the source code for it. Let’s say you want to add an NSString method that …