Contents

Extensions in Objective-C

Extensions in Objective-C

In Objective-C, Extensions (also known as Class Extensions) are a special type of category where methods must be declared within the main implementation block of the associated class. This allows overriding publicly declared property attributes. Unlike regular categories, class extensions can only be applied to classes where the source code is available during compile time, meaning the class and extension are compiled together.

One common use of extensions is to convert a read-only property into a read-write property within a class implementation.

Extensions do not have a name and are often referred to as anonymous categories. These extensions allow developers to add private methods or instance variables to a class. They promote code encapsulation, reusability, and modularity, which are key concepts in object-oriented programming.

Specifications of Extensions
  • Extensions can declare private methods that are specific to a class.
  • Only classes with the source code available at compile time can be extended.
  • Extensions can override publicly declared property attributes.
  • Extensions are frequently used to define private methods or properties used internally in the class implementation.

Syntax for Declaring an Extension

Extensions are declared using the @interface keyword followed by parentheses () without specifying subclass inheritance.

				
					@interface ClassName ()
@end

				
			

Example of an Extension

Here, we define a class Calculator and use an extension to declare a private method.

				
					#import <Foundation/Foundation.h>

// Public interface
@interface Calculator : NSObject

- (int)addNumber:(int)a withNumber:(int)b;

@end

// Class extension for private method
@interface Calculator ()

- (void)displayResult:(int)result;

@end

@implementation Calculator

- (int)addNumber:(int)a withNumber:(int)b {
    int sum = a + b;
    [self displayResult:sum];
    return sum;
}

// Private method implementation
- (void)displayResult:(int)result {
    NSLog(@"The result is: %d", result);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Calculator *calc = [[Calculator alloc] init];
        [calc addNumber:10 withNumber:20];
    }
    return 0;
}

				
			

Output:

				
					The result is: 30

				
			

Example of a Private Extension

Private methods are another common use case for class extensions. By forward-declaring private methods in an extension, we ensure the compiler verifies their existence in the implementation block. Since extensions are defined within the implementation file, they remain hidden from other classes, mimicking private methods.

				
					#import <Foundation/Foundation.h>

// Public interface
@interface MessagePrinter : NSObject

- (void)printMessage;

@end

// Class extension to declare private method
@interface MessagePrinter ()

- (void)prepareToPrint;

@end

// Implementation
@implementation MessagePrinter {
    BOOL _isPrepared;
}

- (void)printMessage {
    if (!_isPrepared) {
        [self prepareToPrint];
        _isPrepared = YES;
    }
    NSLog(@"Hello, Objective-C!");
}

// Private method implementation
- (void)prepareToPrint {
    NSLog(@"Preparing to print...");
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MessagePrinter *printer = [[MessagePrinter alloc] init];
        [printer printMessage];
    }
    return 0;
}

				
			

Output:

				
					Preparing to print...
Hello, Objective-C!