Objective C Extensions - Objective C

What is Extensions in Objective-C?

Extension can be added to a class for which you have the source code at compile time.The class extension bears some similarity to a category.
In a class extension if the methods are declared then it is implemented in the implementation block for the original class.

For Example, declare a class extension on a framework class, such as a Cocoa or Cocoa Touch class like NSString.
If there is no category name then Extensions are categories. And can be referred as anonymous categories.


From the above statements, declaration of extension uses the @interface keyword, like a standard Objective-C class description but does not indicate any inheritance from a subclass. Instead, it just adds parentheses.

Characteristics of extensions

  • Extension can be declared only for the classes that have original implementation of source code.
  • Extension means adding private methods and private variables that are only specific to the class.
  • Any method or variable declared inside the extensions is not accessible even to the inherited classes.

Extensions Example

The below example shows the extension that have a private variable internalID, and method getExternalID which returns the externalID after processing the internalID.

On compiling and executing the above code, the following result is produced:

In the above example, the internalID is not returned directly. Remove the UNIQUEINTERNALKEY and only make the remaining value available to the method getExternalID. here we used a string operation.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Objective C Topics