4

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.

Darshan
  • 2,272
  • 3
  • 31
  • 43

4 Answers4

6

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.

Community
  • 1
  • 1
Rajat
  • 10,977
  • 3
  • 38
  • 55
  • But why category we can create our custom class and achieve this functionlity – Darshan Aug 27 '15 at 12:36
  • 1
    The 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
  • As far as I can tell most categories are used because they are "shiny". – zaph Aug 27 '15 at 12:45
  • They also ease the burden of maintaining large code. – Rajat Aug 27 '15 at 12:47
  • 2
    If 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
5

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

1

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.

Kalyani
  • 392
  • 2
  • 17
1

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.

Paul
  • 156
  • 9