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.
-
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1 – Akusete Aug 17 '10 at 06:09
-
3Akusete, page cannot be found. – Adil Hussain Aug 26 '14 at 13:08
-
clearly illustrated here, http://code.tutsplus.com/tutorials/objective-c-succinctly-categories-and-extensions--mobile-22016 – grassyburrito Apr 28 '15 at 13:42
10 Answers
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:
The category has no name. It is declared like this:
@interface SomeClass ()
@end- (void) anAdditionalMethod;
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).

- 242,470
- 58
- 448
- 498
-
13In 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
-
1Why 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 '14 at 17:40
-
1@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 '14 at 08:08
-
@MaxSeelemann can you please demo how to make "a (public) read-only property to be internally readwrite" by a short script? Thanks. – allenlinli Jan 16 '17 at 00:00
-
[This answer](http://stackoverflow.com/a/33942484/3798182) states extension in "Class+CategoryName.swift" file, is it correct ?? – Ritesh.mlk Apr 17 '17 at 11:48
-
@Dave DeLong [Apple Documentation on Category](https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html) states "The declaration of a category interface looks very much like a class interface declaration—except that the **category name is listed within parentheses** after the class name and the superclass isn’t mentioned" – Ritesh.mlk Apr 17 '17 at 12:21
-
@Rishi That's talking about a category. The question was asking about a class extension, not a category. A class extension does not have a name. – Dave DeLong Apr 17 '17 at 23:38
-
@DaveDeLong Sir your 1st point says **"The category has no name. It is declared like this:"** But Category has a name and it is not declared like this, Am i missing something sir ? I am totally confused – Ritesh.mlk Apr 18 '17 at 03:38
-
@Rishi it's saying that a class extension is like a category, except that the "category" for a class extension "has no name" – Dave DeLong Apr 19 '17 at 16:36
-
@DaveDeLong Unlike Objective-C categories, Swift extensions do not have names. – Ritesh.mlk Apr 20 '17 at 05:42
-
@Rishi what does Swift have to do with anything? There's no Swift anywhere in this question and answer, because it's from 2010, about 4 *years* before Swift was released. – Dave DeLong Apr 24 '17 at 21:40
-
@DaveDeLong When you say "category" for a class extension "has no name". you are referring to extension?, because Apple Documents says **"Unlike Objective-C categories, Swift extensions do not have names"**. Which means Objective-C categories have names. yes the question was asked in 2010, But unfortunately I have to give interviews in 2017. I am sorry for asking it again. Kindly refer me a place, book or website where i can clear my basics of Objective-C. Thanks a lot – Ritesh.mlk Apr 25 '17 at 03:32
- 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.
- 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.

- 9,459
- 3
- 32
- 39

- 2,379
- 26
- 32
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.
We can add extra instance variables and properties in class extension but not in category.
Any variable and method inside the extension is not even accessible to inherited classes.
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.
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.

- 9,564
- 146
- 81
- 122

- 5,672
- 42
- 41
-
3I read 5 similar questions with many answers, and this is the best. Thank you! – mfaani Mar 28 '16 at 20:43
-
1
-
Why - Any variable and method inside the extension is not even accessible to inherited classes.? Can elaborate this? – Jayprakash Dubey Jun 07 '21 at 20:06
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
.
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/

- 617
- 7
- 10
@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
-
This is how you declare and extension, not a category -> @interface SomeClass () – ChavirA Apr 14 '16 at 17:51
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

- 1,643
- 3
- 23
- 30

- 364
- 1
- 5
ios extension similiar to c#,java abstract class or interface
ios category similiar to c#,java class extension

- 1,818
- 1
- 18
- 25
-
4your answer just like "cat is similar to tiger, answering a blind man". Lol – Muruganandham K Feb 11 '16 at 06:27
-
1it is not actually. It is the most direct and fast explanation if you know c# or java. – Add080bbA Feb 12 '16 at 11:00
-
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.

- 958
- 1
- 14
- 23
-
-
@iPhoneDeveloper We can write the extension for predefined types. Please check this link - https://stackoverflow.com/a/33942484/2060508 – yo2bh Jun 28 '18 at 16:39
Categories and Extensions
A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Using categories, you can also distribute the implementation of your own classes among several files. Class extensions are similar, but allow additional required APIs to be declared for a class in locations other than within the primary class @interface block.
Adding Methods to Classes
You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.
The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category are included as methods the compiler expects an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass, however, are not included in the NSArray type. (This matters only for statically typed objects because static typing is the only way the compiler can know an object’s class.)
Category methods can do anything that methods defined in the class proper can do. At runtime, there’s no difference. The methods the category adds to the class are inherited by all the class’s subclasses, just like other methods.
The declaration of a category interface looks very much like a class interface declaration—except the category name is listed within parentheses after the class name and the superclass isn’t mentioned. Unless its methods don’t access any instance variables of the class, the category must import the interface file for the class it extends:
#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end
Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.
There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.
Extensions
Class extensions are like anonymous categories, except that the methods they declare must be implemented in the main @implementation block for the corresponding class. Using the Clang/LLVM 2.0 compiler, you can also declare properties and instance variables in a class extension.
A common use for class extensions is to redeclare property that is publicly declared as read-only privately as readwrite:
@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end
// Notice that (in contrast to a category) no name is given in the parentheses in the second @interface block.
It is also generally common for a class to have a publicly declared API and to then have additional methods declared privately for use solely by the class or the framework within which the class resides. Class extensions allow you to declare additional required methods for a class in locations other than within the primary class @interface block, as illustrated in the following example:
@interface MyClass : NSObject
- (float)value;
@end
@interface MyClass () {
float value;
}
- (void)setValue:(float)newValue;
@end
@implementation MyClass
- (float)value {
return value;
}
- (void)setValue:(float)newValue {
value = newValue;
}
@end
The implementation of the setValue: method must appear within the main @implementation block for the class (you cannot implement it in a category). If this is not the case, the compiler emits a warning that it cannot find a method definition for setValue:. For official documentation follow this link: source

- 795
- 9
- 16