Reputation: 2379
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.
Upvotes: 4
Views: 3544
Reputation: 156
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.
Upvotes: 1
Reputation: 11127
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.
Upvotes: 6
Reputation: 392
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.
Upvotes: 1
Reputation:
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
How to use category in obj video
Upvotes: 5