I have just starting developing application in ios and i have read so many tutorial for that but still i have not satisfy from that tutorial. i want to know theoretical why we need to use category and what are the benefits.
-
http://stackoverflow.com/a/24568948/3767017 have a look to this answer – Anurag Bhakuni Aug 27 '15 at 12:56
4 Answers
Referenced from
http://www.g8production.com/post/37787310116/categories-in-objective-c-how-to-extend-methods
and
Difference between Category and Class Extension?
Categories and extensions allow you to extend the functionality of existing classes without subclassing (inherit nothing) adding functionality to an existing class, even to one for which you do not have the source.
A category allow you to add (only) methods to a class by declaring them in an interface file (.h) and defining them in an implementation file (.m), like in a basic Objective-C class. Sadly a category can’t declare additional instance variable for a class.
Now this declared methods become part of the categorized-class!!!
There’s no limit to the number of categories that you can add to a categorized-class, but each category name must be different should declare and define a different set of methods.
Edit
Categories
-> In objective c, when you want to add some more functionality to a class without inheritance, you simply use category for it.
-> Category use to add new method not properties
.
Class Extension
-> In objective c, when you want to make behavior of some property private you use class extension.
->mainly for properties.
-
But why category we can create our custom class and achieve this functionlity – Darshan Aug 27 '15 at 12:36
-
1The statement "inherited by all *subclasses* of the categorized-class" is incorrect, the methods become part of the class the category is on, not just subclasses. As such they polite the class for the entire app. Categories tend to be overused, in most cases an instance to class method is more appropriate. – zaph Aug 27 '15 at 12:41
-
-
-
2If you scrape text from other places as an answer, it is polite to link to the original sources. http://www.g8production.com/post/37787310116/categories-in-objective-c-how-to-extend-methods and http://stackoverflow.com/questions/3499704/difference-between-category-and-class-extension – Rory McKinnel Aug 27 '15 at 12:54
A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing
Benefit of using categories. if you use categories then no need to keep in mind which custom class you have created for that specific feature. simply by using categories you can add new capability to existing class & by creating object of same class u can access it.

- 392
- 2
- 17
In one of my apps I wanted to change the default font. So instead of constantly changing the font where ever there was text, I set up a category were it changed the default font to the one I wanted.

- 156
- 9