Categories were introduced in Objective-C If we want to add any new functionalities to the native classes as well as to our own custom classes.
If the above statement is sounding new to you, Please check my
Objective C Subclass vs Category post, Which gives a clear idea to you on when to subclass and when to categorize.
Let's see some cases where we have to go for Categories instead of Subclassing by taking NSString class.
I want string reversal at various places in my application.
I want number of spaces in a string.
In this case, We have two options.
#1 Write a subclass of NSString and add these two methods.
If I subclass and write my custom methods,
I need to import that subclass header file wherever I need those methods.
And I need to instantiate my subclass everywhere to use those methods.
Isn't it painful for a matter of adding two new methods writing a sub class and using it?
What if there is something which allows us to add these two new methods in the scope of native class itself and allows us to call directly using NSString object.
In your Xcode editor, Go to,
File -> New -> select 'Objective-C File'
You will be getting,
File : Enter the category name of your choice. In my case it is 'Additions', as there are additions to NSString.
File Type : Category
Class : The base class for our categorization. In our case, It is NSString.
will get created in your project.
The file names them self convey the reason behind their creation. We have taken those files to add our custom functionality to the NSString class. So wherever we need those new custom methods, We need to import
Let's add our custom methods now.
Whatever the category name we have given in the File box, appears in the brackets.
Here, NSString (Additions). You can keep your own name.
They could be a better logic for string reversal.
Please ignore my logic for reversing a string though it's working :)
We have added our required custom methods to the NSString class. Let's see how to access them.
Wherever you need these methods, import NSString+Additions.h file and you can access them as simple as the native NSString methods as shown in the below example.
Hope this post is useful. Feel free to comment incase of any queries.