Pages

Sunday 28 October 2012

Objective-C categories

Before we start let us make a point clear, Objective-C categories and Extensions are not the same and dont get confused on these terms.

Alright, Lets start.

Categories are used to create new abilities to an existing class.

 For example, let us take NSString class. If we need to add the ability to strip out all the special character from an NSString, the best way to follow is category. For this purpose we can simply create a category of NSString and add the method we need.

By doing this we are not actually changing any of the basic characteristics of an existing class but adding new features to it.

Now let us discuss about extension. It is always advised to subClass an existing class when ever we need a different behavior in the subClass when compared to parent class.

Hope thats clear. :) 

No comments:

Post a Comment