89

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?

AWF4vk
  • 5,810
  • 3
  • 37
  • 70
  • categories 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. – Fattie Dec 03 '13 at 13:03
  • For extensions as they are used in Swift, see this question: http://stackoverflow.com/questions/24142829/how-to-create-swift-class-for-category – Suragch Jun 24 '15 at 12:37

4 Answers4

95

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.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 42
    An 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. – jtbandes Aug 21 '11 at 03:55
  • 1
    You can also add additional storage is class extensions using properties. – Paul.s Aug 21 '11 at 09:49
  • 25
    Class extensions were also specifically designed to allow a property to be publicly `readonly` and privately `readwrite`. Can't do that with categories (a conscious design choice). – bbum Aug 17 '12 at 17:37
  • Why Google is using category to implement private methods instead of extension? e.g. http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/Networking/GDataServerError.m – Ryan Aug 18 '12 at 01:44
  • extensions 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 – Ankit Thakur Aug 18 '12 at 11:20
  • It 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. – Chris Conover Nov 08 '13 at 05:06
  • You 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. – Christopher Pickslay Dec 05 '13 at 07:07
  • @chrisco what you mean? 1. Can't I write a extension for system class ex NSObject(I don't have source code for nsobject)? – jailani Apr 20 '14 at 17:55
  • @IOSDeveloper: You might be thinking of sub-classing, but full explanation can be found [here](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW3), 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...". – Chris Conover Apr 21 '14 at 23:02
  • @bbum..Is this what u mean? http://stackoverflow.com/questions/4837565/objective-c-private-public-properties-making-a-property-readonly-for-outsi If not please give an example. – Imran Jul 28 '15 at 11:16
  • If 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 Oct 07 '18 at 19:57
  • Is [this](https://stackoverflow.com/questions/3967187/difference-between-interface-definition-in-h-and-m-file#comment26723238_3967436) the answer to my question? – mfaani Oct 07 '18 at 20:05
  • One 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 in `Class_Private.h` and your truly internal crap in either `Class_Internal.h` (or Class.m) and use Xcode's header visibility inspector to set how exposed the header files are. – bbum May 14 '19 at 18:14
32

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.

John Doe
  • 2,173
  • 1
  • 21
  • 12
7

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.

Julie Yu
  • 83
  • 1
  • 6
-1

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

Add080bbA
  • 1,818
  • 1
  • 18
  • 25