In this post we shall have a look at the concept of categories.
Categories: Sometimes while working with a particular class in a project you would have wished that if only there was some way in which you could add some methods of your own in the class which already exists in the framework.
For example say you have learned how to use NSStrings or NSArray which are the class present in the foundation framework and realize that you wish the class had implemented one or two methods which you wanted as per your applications business logic. In that case the only one thing that you would do is create a subclass of the NSArray or NSString but that is not the case with Objective C.
One Solution present for this is categories. A category is a methodology in Objective c with the help of which you can extend an existing class definition without having the access to the original source code for the class and without having to create a subclass. This means that say if I want to add a method of my own in a class that already is a member of the foundation framework then I can easily do that with the help of categories.
Let’s have a look at an example where I will be adding a function inside the NSString class which will accept a parameter of string type and that function will reverse the string passed as a parameter and will return you the reversed string, so now in my project class whenever I want to reverse a string I will just add a category once which would contain a method to reverse a string inside the NSString class and then I could use this reverse string function just like any other function of the NSString class inside my application.
Note: The category that you are adding for a particular project say A will work only for that project and not for project B, for Project B you have to again create the category.
Syntax of category: The image given below is the syntax which describes how you describe a category for a particular class which is very quite similar to a class declaration with the only difference that it lacks the curly braces.
@interface NSString (MyString)
The above line tells the compiler that you are defining a new category for the NSStirng class and the name of that category is MyString. The category name is enclosed in a pair of parentheses after the class name. The MyString category contains an instance function with the help of which you will get your reversed string.
Syntax to implement the category: Once the category is declared its time to implement the category which is very similar to the class implementation in objective c.
Once the category is defined then you can use the category, given below is the image with the help of which displays how you can use the category.
Code Explanation: Now when there is a call to the above method it makes a call to the reverse string category method and the appropriate code is executed and you get your reversed string. This is how you work with categories in objective c.
I hope that you have understood the concept of categories in objective c in case if you are having any issues or any queries then in that case feel free to ask your queries as a comment or you can mail me at firstname.lastname@example.org until then happy iCoding and have a great day, and you can get the PDF of this post here.