Categories in Objective-C
Last Updated :
26 Apr, 2025
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.
Similar Reads
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Constants in Objective-C
Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
Constructors in Objective-C
Constructor is basically a function or method that is called automatically at the time of object creation of a class. constructor method has the same name as that of the class. It is basically used to initialize the data members of the new objects generally. It constructs or creates a new value for
8 min read
Extensions in Objective-C
In contrast to an implementation described in a category, Extensions (Class Extensions) are a specific kind of category that necessitates that its methods be declared in the main implementation block for the associated class. Publicly declared property attributes may be overridden with this. Similar
3 min read
Type Casting in Objective-C
Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
for loop in Objective-C
Like most programming languages, Objective-C also has a repeating statement called for loop. It is a repetition control statement that allows you to repeatedly execute a single instruction or a block of instructions into a specified number of times. unlike other loops like while-loop or do-while loo
9 min read
Blocks in Objective-C
Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us
6 min read
Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 min read
Error Handling in Objective-C
In this article, we will learn about error handling in objective C. There are different methods to handle errors in Objective-C with the help of examples and code. What is Error Handling?Error handling is defined as the process or mechanism in programming languages for identifying, catching, and res
6 min read
Class Hierarchy in Objective-C
Objective-C is an effective programming language this is broadly used for growing applications on Apple's macOS and iOS platforms. Objective-C is an object-oriented programming language, hence, providing the capability of the class hierarchy. Class hierarchy means acquiring features of the base clas
5 min read