Abhinav
Abhinav

Reputation: 38162

Difference between Category and Class Extension?

What is the difference between a Category and a Class Extension. I believe both are used to add custom methods in existing classes. Can someone throw light on this? Examplification with code will be really appreciated.

Upvotes: 90

Views: 58372

Answers (9)

Anurag Bhakuni
Anurag Bhakuni

Reputation: 2439

  1. Category

=> In Objective C, when you want to add some more functionality to a class without inheritance, you simply use category for it.

=> it comes with its own .h and .m file

=> Category uses to add new method not properties.

  1. Class Extension
-> In Objective C, when you want to make behaviour of some property private you use class extension.

-> it comes with **.h** file only.

-> mainly for properties.

Note: when we add a new file and select a option of objective c category shows category and "category on" not "subclass of" so it shows like

@interface className (categoryName)
@end

-You will get two file .h and .m with file name as (className+categoryName.h and className+categoryName.m)

and in extension case you will get

@interface className()
@end

-You will get only one file with name as className_extensionName.h

  • In category you don't own the class but in extension you are.

Upvotes: 49

Venu Gopal Tewari
Venu Gopal Tewari

Reputation: 5876

  1. Category is a way to add methods to a class whether or not source code is available, meaning you can add category to foundation classes like NSString and also to your own custom classes.

    Extension can only be added to the classes whose source code is available because compiler compiles the source code and extension at the same time.

  2. We can add extra instance variables and properties in class extension but not in category.

  3. Any variable and method inside the extension is not even accessible to inherited classes.

  4. Category and extension both are basically made to handle large code base, but category is a way to extend class API in multiple source files while extension is a way to add required methods outside the main interface file.

  5. Use category when you have to break your same class code into different source files according to different functionalities, and extension when you just need to add some required methods to existing class outside the main interface file. Also, when you need to modify a publicly declared instance variable in a class, for example, readonly to readwrite, you can re-declare it in extension.

Upvotes: 37

garg
garg

Reputation: 2727

Extension: To make methods private and to add properties of our own custom class, not of Apple class.

Category: To add more methods in existing class not the property, it can be used for both custom class and Apple class like NSString.

Upvotes: 7

iPhoneDeveloper
iPhoneDeveloper

Reputation: 996

Here is my understanding :

Extensions are usually used to add extra features to our own "custom class". We can add private methods or properties extending the class interface which can be used within the implementation of the class.

Extensions are to be written within the same file as the class. Hence you cannot write extensions for pre defined types like String, Float, etc.

On the other hand Categories can be used to add extra methods to a pre existing classes. Example we can create our own methods by extending String class. Note that we cannot create extra properties in the categories. Also main advantage of categories is we can write the categories in any other file, outside the file where your class exits.

Also while creating categories you are supposed to give a name for it within the brackets. But for extension no name is required. Hence some times they are also called anonymous categories.

Upvotes: 0

Suryanarayan Sahu
Suryanarayan Sahu

Reputation: 364

Categories

Categories are used when you are creating file containing large number of methods.So they provide you with the facility to break a single class into different modules.Also if any changes are made to the categories the compiler does not waste time to compile the entire project.Categories are not able to add new variable or properties and look upto their parent class .You can override a method in a category but it isnt a good idea because the method cannot further be overridden.Also the flow can be effected because all categories have the same hierarchial level and hence two categories belonging to same parent class may exist at run time.Also protected methods can be created using categories

Extensions

Extensions enable you to override the property or add new property to the existing parent class.Syntatically same to categories they do not have name and are represented as @interface class() No .m file is present and method declared in extension have to be implemented in @implementation of parent file

More help at this link

Upvotes: 0

Add080bbA
Add080bbA

Reputation: 1876

ios extension similiar to c#,java abstract class or interface
ios category similiar to c#,java class extension

Upvotes: 0

PradeepKN
PradeepKN

Reputation: 667

We can also have properties Using set associated property in category class.

@interface SomeClass (Private)

@property (nonatomic, assign) id newProperty;

@end

NSString * const kNewPropertyKey = @"kNewPropertyKey";

@implementation SomeClass (Private)

@dynamic newProperty;

- (void)setNewProperty:(id)aObject
{
    objc_setAssociatedObject(self, kNewPropertyKey, aObject, OBJC_ASSOCIATION_ASSIGN);
}

- (id)newProperty
{
    return objc_getAssociatedObject(self, kNewPropertyKey);
}

@end

Refer : http://inchoo.net/dev-talk/ios-development/how-to-add-a-property-via-class-category/

Upvotes: 3

Exploring
Exploring

Reputation: 935

@interface SomeClass ()


- (void) anAdditionalMethod;


@end

I think it is not the way to declare Category. Category must have a name

@interface SomeClass (XYZ)


- (void) anAdditionalMethod;


@end

for example

@interface NSMutableArray (NSMutableArrayCreation)

+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;

@end

Declared for NSMutableArray by Apple

Upvotes: 2

Dave DeLong
Dave DeLong

Reputation: 243156

A category is a way to add methods to existing classes. They usually reside in files called "Class+CategoryName.h", like "NSView+CustomAdditions.h" (and .m, of course).

A class extension is a category, except for 2 main differences:

  1. The category has no name. It is declared like this:

    @interface SomeClass ()
    
    
    - (void) anAdditionalMethod;
    
    @end
  2. The implementation of the extension must be in the main @implementation block of the file.

It's quite common to see a class extension at the top of a .m file declaring more methods on the class, that are then implemented below in the main @implementation section of the class. This is a way to declare "pseudo-private" methods (pseudo-private in that they're not really private, just not externally exposed).

Upvotes: 107

Related Questions