articles

Home / DeveloperSection / Articles / Objective-C : Categories

Objective-C : Categories

Tarun Kumar2936 17-Jul-2015

Previously we learn encapsulation of data with methods : Objective-C : Data Encapsulation


 Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. In order add such extension to existing classes, Objective-C provides categories and extensions.


If you need to add a method to an existing class, perhaps, to add functionality to make it easier to do something in your own application, the easiest way is to use a category.

The syntax to declare a category uses the @interface keyword, just like a standard Objective-C class description, but does not indicate any inheritance from a subclass. Instead, it specifies the name of the category in parentheses, like this:

@interface ClassName (CategoryName)

@end

Characteristics of category

·   A category can be declared for any class, even if you don't have the original implementation source code.

·    Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class.

·    At runtime, there's no difference between a method added by a category and one that is implemented by the original class. 


In the example given below we have a class BaseClass that has some methods and the second class SubClass that is used to add a method to the BaseClass. In the main, we have created object of base class and use the method defined in the sub class. 

Example:

This is code of primary class.

BaseClass.h


#import<Foundation/NSObject.h>

@interface BaseClass : NSObject {

    int num1, num2;

  }

  -(void)set :(int) x and: (int) y;

  -(int)add;

  -(int)sub;

@end


BaseClass.m


#import"BaseClass.h" 
@implementation BaseClass
  -(void)set :(int) x and: (int) y {
  num1 = x;
  num2 = y;
  }
  -(int)add {
  return num1+num2;
  }
  -(int)sub {
  if(num1>num2){
  return num1-num2;
   }
  else
  return num2-num1;
   }
@end

 

This is code of sub class that is used to add method in the primary class.

 

SubClass.h

#import"BaseClass.h"

@interface BaseClass(Category)

  -(void)show:(int)x;

@end


SubClass.m


#import"SubClass.h"

@implementation BaseClass(BaseClass)

  -(void)show:(int)x {

      printf("Result is : %d \n",x);

  }

@end

 

main.m


#import"BaseClass.m"

#import"SubClass.m"

#import<stdio.h>

 

int main(){

   BaseClass *obj = [[BaseClass alloc] init];

   [obj set:10 and:8];

   [obj show:[obj add]];

   [obj show:[obj sub]];

 

   [obj release];

   return 0;

}

 

Output:


Result is : 18

Result is : 2


 Notes:

·     A category method can override an existing method, but you’ll lose access to

the original outside of that class. Therefore, if you do override, make sure you

duplicate existing functionality, or use a call to super.


·    If a method is defined in more than one category of a single class, which version

is invoked is undefined.

·    Remember that methods that you add to a class are inherited as well. If you add

methods to NSObject, every class will have access to those methods.

·     Added methods may serve your purpose, but may go against the design of the

class. Adding the ability to handle multiple strings simultaneously to NSString blurs

the line between NSString and NSArray.

·    Object / category name pairs must be unique in any namespace. There can only

be one NSArray-RandomObjects in a namespace. The default namespace is shared

between all classes, libraries, frameworks, and plug-ins. This can be a hazard for

developers of those products, especially since they get injected into other code.

 

Next, we will learn about : Objective-C : Posing

 


Updated 13-Dec-2017

Leave Comment

Comments

Liked By