Open In App

Categories in Objective-C

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Categories are an important concept in the Objective-C programming language. They allow developers to extend the functionality of existing classes without having to modify their original code. This article will discuss categories in Objective-C, their uses, and provide examples of their implementation.

Categories

In Objective-C, categories are a way to add new methods and properties to existing classes without subclassing them. This means that categories can be used to extend the functionality of a class without having to write additional code. Categories can also be used to separate the code for a class into different logical sections, making the code easier to read and maintain.

In order to use categories in Objective-C, the first step is to define a category. This is done by creating a header file with the same name as the class that is being extended, followed by the word “Category”. The header file should then contain the @interface block for the category, as well as the @implementation block. The @interface block should contain a list of the new methods and properties that are being added to the class. The @implementation block should contain the code for the new methods and properties.

Syntax:

@interface MyClass (MyCategory)

// Method declarations go here

@end

Let’s look at an example of how categories can be used in Objective-C. Below are the steps for an example:-

Step 1: Suppose we have a class called MyClass and we want to create a category called “MyCategory” that adds a new method and property to the class. We would create a header file called MyClass+MyCategory.h and define the @interface block like this:

@interface MyClass (MyCategory)

@property (nonatomic, strong) NSString *myString;

– (void)sayHello;

@end

Step 2: We would then implement the new method and property in the @implementation block, like this:

@implementation MyClass (MyCategory)

@synthesize myString;

– (void)sayHello {

NSLog(@”Hello!”);

}

@end

Step 3: Once the category has been implemented, it can be used to extend the functionality of MyClass. For example, we could call the sayHello method like this:

MyClass *myObject = [[MyClass alloc] init];

[myObject sayHello];

Example:

ObjectiveC




// Objective-C program for categories
#import <Foundation/Foundation.h>
  
// Creating category
@interface NSString(mynumber)+(NSString *)getnumber;
@end
  
// Implementing the method
@implementation NSString(Mynumber) +(NSString *)getnumber 
{
   return @"Getting the number 111 and function is successfully executed";
}
@end
  
int main(int argc, const char * argv[]) 
{
   NSAutoreleasePool * temp = [[NSAutoreleasePool alloc] init];
   NSString *getnumber = [NSString getnumber];
   NSLog(@"output: %@",getnumber);
   [temp drain];
   return 0;
}


Output:

Getting the number 111 and function is successfully executed

Categories are a powerful and useful feature of the Objective-C language. They allow developers to add new methods and properties to existing classes without having to write additional code or modify the original source code. Categories can also be used to logically separate the code for a class into different sections, making it easier to read and maintain.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads