Take the 2-minute tour ×

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.

share|improve this question
Akusete, page cannot be found. –  Adil Hussain Aug 26 at 13:08

3 Answers 3

up vote 47 down vote accepted

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;
  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).

share|improve this answer
Thank you so much. –  Abhinav Aug 17 '10 at 8:26
In Class-Extensiosn you're able to overwrite properties from a (public) read-only property to be internally readwrite. And since LLVM 1.5 you're able to do even more: you can now declare instance variables in a class extension so they are gone from the regular interface. –  Max Seelemann Aug 17 '10 at 13:53
Why add a class extension when you could just implement those methods and properties strait into the class? Seems a little redundant to me. –  user1721803 Dec 16 at 17:40
@user1721803 As Dave suggested you might want to keep few properties/methods unexposed. This is when extensions are used. Consider a view, you could hook up all your IBOutlets and IBActions in extension and expose methods which allows to initialize/update Outlets and Actions could be delegated back. –  GoodSp33d Dec 22 at 8:08

-> In objective c, when you want to add some more   functionality to a class without inheritance, you simply use category for it.

-> it come with its own .h and .m file 

-> Category use to add new method not properties.

2.class extension

-> In objective c, when you want to make behavior of some property private you use class extension.

->its come with .h file only.

->mainly for properties.

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

@interface className (categoryName)

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

and in category case you get

@interface className()

-you get only one file with name as className_extensionName.h

* In category you dont own the class but in extension you are.
share|improve this answer
@interface SomeClass ()

- (void) anAdditionalMethod;


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

@interface SomeClass (XYZ)

- (void) anAdditionalMethod;


for example

@interface NSMutableArray (NSMutableArrayCreation)

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


Declared for NSMutableArray by Apple

share|improve this answer

Your Answer


