Open In App

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 to categories, only classes for which you have the source code at compile time (the class is compiled at the same time as other classes) are eligible for class extensions. For instance, in the implementation of a class, it may be practical to convert a read-only property to a read-write property.

The implementation block for the original class contains the implementation of the methods declared by a class extension. In reality, extensions are just categories without a name. It’s frequently called “anonymous categories.” One of the core concepts of object-oriented programming is class extension. Classes can be expanded by being altered, expanded, or abstracted, and they contain instance variables and methods. The extension of classes encourages code reuse and compartmentalization.



Specifications of Extensions

Syntax to declare Extension

The @interface keyword is used in the syntax to specify an extension, but no indication of subclass inheritance is made. It only adds parenthesis in its place.

@interface NameofClass ()



@end

Example of Extension

Here, we have created a class called MyProgram with an extension.




#import <Foundation/Foundation.h>
@interface MyProgram:NSObject
  
@end
  
// Class extension
@interface MyProgram()
  
// private method
- (void)myMethod;
  
@end
  
@implementation MyProgram
  
- (void)myMethod{
  NSLog(@"geeksforgeeks");
}
  
@end
  
int main(int argc, const char * argv[])
{
    
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        MyProgram *myProgram= [[MyProgram alloc] init];
        [myProgram myMethod];
        [pool drain];
      
   return 0;
}

Output:

 

Example of Private Extension

The declaration of private methods is another frequent application of extensions. Forward-declaring a private method with a class extension was the standard procedure for doing so.

It serves as a forward declaration because the compiler makes sure the extension methods are implemented in the main implementation section. But, since the extension is contained in the implementation file, other objects shouldn’t ever be aware of it, providing us with yet another means to simulate private methods. Although newer compilers relieve you from this hassle, it’s still vital to understand how class extensions function because, up until very recently, it was a frequent technique to use secret methods in Objective-C programs.




#import <Foundation/Foundation.h> 
@interface Program : NSObject
- (void)print;
@end
  
// The class extension. 
@interface Program()
- (void)prepareToExecute;
@end
  
// The rest of the implementation. 
@implementation Program
BOOL _IsReady;
- (void)print {
        if (!_IsReady) {
        [self prepareToExecute];
        _IsReady = YES;
    }
    NSLog(@"HEY");
}
- (void)prepareToExecute {
    // Executing some private functionality. 
    NSLog(@"geeksforgeeks");
}
@end
int main(int argc, const char * argv[])
{
    
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        Program *program= [[Program alloc] init];
        [program prepareToExecute];
        [pool drain];
      
   return 0;
}

Output:

 

Summary


Article Tags :