Open In App

Extensions in Objective-C

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Extensions can be used to add private methods that are unique to a class.
  • Only classes for which we have the original source code implementation are eligible for extension declaration.
  • Publicly declared property attributes may be overridden with this.
  • Class extensions are frequently used to introduce new private methods or attributes to the public interface for usage in the class’s implementation.

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.

ObjectiveC




#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:

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.

ObjectiveC




#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:

output

 

Summary

  • Extensions were one of the topics covered in this chapter about the Objective-C programming language. 
  • Extensions are a means to extend the API outside of the primary interface file with necessary functions.
  • Private variables and methods that are exclusive to the class are added as an extension.
  • Even the inherited classes cannot access any methods or variables declared inside the extensions.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads