Class extension vs class category
Solution 1
The main difference is that with an extension, the compiler will expect you to implement the methods within your main @implementation
, whereas with a category you have a separate @implementation
block. So you should pretty much only use an extension at the top of your main .m
file (the only place you should care about ivars, incidentally) -- it's meant to be just that, an extension.
Solution 2
A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time (the class is compiled at the same time as the class extension). The methods declared by a class extension are implemented in the @implementation block for the original class so you can’t, for example, declare a class extension on a framework class, such as a Cocoa or Cocoa Touch class like NSString.
The syntax to declare a class extension is similar to the syntax for a category, and looks like this:
@interface ClassName ()
@end
Because no name is given in the parentheses, class extensions are often referred to as anonymous categories.
Unlike regular categories, a class extension can add its own properties and instance variables to a class. If you declare a property in a class extension, like this:
@interface XYZAnimal () {
id _someCustomInstanceVariable;
}
...
@end
IMHO, it's best to think of class extensions as private interface to a class. The primary interface (in your .h file) acts as the public interface which defines the class's behavioural contract with other classes.
Use class extensions to Hide Private Information
Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself. It’s common, for example, to define a property as readonly in the interface, but as readwrite in a class extension declared above the implementation, in order that the internal methods of the class can change the property value directly.
As an example, the XYZPerson class might add a property called uniqueIdentifier, designed to keep track of information like a Social Security Number in the US.
It usually requires a large amount of paperwork to have a unique identifier assigned to an individual in the real world, so the XYZPerson class interface might declare this property as readonly, and provide some method that requests an identifier be assigned, like this:
@interface XYZPerson : NSObject
...
@property (readonly) NSString *uniqueIdentifier;
- (void)assignUniqueIdentifier;
@end
In order for the XYZPerson class to be able to change the property internally, it makes sense to redeclare the property in a class extension that’s defined at the top of the implementation file for the class:
@property (readwrite) NSString *uniqueIdentifier;
Note: The readwrite attribute is optional, because it’s the default. You may like to use it when redeclaring a property, for clarity.
Solution 3
Categories are an Objective-C language feature that let you add new methods to an existing class. Extensions are a special case of categories that let you define methods that must be implemented in the main implementation block.
Private declarations can be in class extensions, which mainly are some properties, because we have no need to declare a method before we call it.
Related videos on Youtube
Comments
-
AWF4vk about 4 years
Class extensions
@interface Class ()
are a lot more powerful and can inject variables into the class. Categories@interface Class (Category)
can't.What other differences are there, and when should one use a category over a class extension?
-
Fattie over 10 yearscategories are actual code. they're how you add features to a class. extensions are (in very general terms) more just syntactical sugar, to signal certain ideas about privacy, etc, to other programmers. extensions contain no code and are not code.
-
Suragch about 9 yearsFor extensions as they are used in Swift, see this question: stackoverflow.com/questions/24142829/…
-
-
csiu almost 13 yearsAn extension is best for private methods which you would like to declare in your
.m
file. I use it for this all the time. Categories are more useful when you want to group your methods into different sections — categories :) — or when you want to add code to existing classes that you didn't create. -
Paul.s almost 13 yearsYou can also add additional storage is class extensions using properties.
-
bbum almost 12 yearsClass extensions were also specifically designed to allow a property to be publicly
readonly
and privatelyreadwrite
. Can't do that with categories (a conscious design choice). -
Ryan almost 12 yearsWhy Google is using category to implement private methods instead of extension? e.g. code.google.com/p/gdata-objectivec-client/source/browse/trunk/…
-
Ankit Thakur almost 12 yearsextensions requires all the methods to be implemented in @implementation block, also it provides enhancement for variables. But we are basically trying to put some of the private methods instead of using private variables, so in that case we are preferring category instead of extension
-
Chris Conover over 10 yearsIt should also be noted that you can only write an extension to a class for which you have the source code. In contrast, you can apply a category to any system class. It is also worth noting that you can achieve the appearance of new properties in categories via the objc_getAssociatedObject and objc_setAssociatedObject runtime functions.
-
Christopher Pickslay over 10 yearsYou can also use a class extension to make private methods visible to other classes, like test cases. Just add the class extension to the test class and re-declare the method.
-
jailani about 10 years@chrisco what you mean? 1. Can't I write a extension for system class ex NSObject(I don't have source code for nsobject)?
-
Chris Conover about 10 years@IOSDeveloper: You might be thinking of sub-classing, but full explanation can be found here, search for "A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time...".
-
Imran almost 9 years@bbum..Is this what u mean? stackoverflow.com/questions/4837565/… If not please give an example.
-
someUser over 8 yearsJava does not support extension methods
-
Add080bbA over 8 yearssorry ! Copy-paste typo. ios category similiar to c# class extension .
-
Fawkes over 8 years"that must be declared in the main implementation block." -> that must be "implemented" in the main implementation block
-
Julie Yu over 8 yearsThanks, I've modified it.
-
mfaani over 5 yearsIf it's private then why I would ever need to place a function in my
@interface
?! What would I lose by removing it? Or is that for functions it offers no benefit, rather it's useful only for adding private iVars? -
mfaani over 5 yearsIs this the answer to my question?
-
bbum about 5 yearsOne correction. Class extensions were specifically designed to help support the "public vs. private vs. internal" nature of APIs when implementing framework based code. I.e. you put your primary
@interface
in Class.h, your "should only be used by shared innards" in an extension inClass_Private.h
and your truly internal crap in eitherClass_Internal.h
(or Class.m) and use Xcode's header visibility inspector to set how exposed the header files are. -
Pedro Trujillo almost 5 yearsCan you add an example about ?