Class extension vs class category

61,076

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.

Share:
61,076

Related videos on Youtube

AWF4vk
Author by

AWF4vk

(your about me is not currently blank)

Updated on May 28, 2020

Comments

  • AWF4vk
    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
      Fattie over 10 years
      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.
    • Suragch
      Suragch about 9 years
      For extensions as they are used in Swift, see this question: stackoverflow.com/questions/24142829/…
  • csiu
    csiu almost 13 years
    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.
  • Paul.s
    Paul.s almost 13 years
    You can also add additional storage is class extensions using properties.
  • bbum
    bbum almost 12 years
    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).
  • Ryan
    Ryan almost 12 years
    Why Google is using category to implement private methods instead of extension? e.g. code.google.com/p/gdata-objectivec-client/source/browse/trun‌​k/…
  • Ankit Thakur
    Ankit Thakur almost 12 years
    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
  • Chris Conover
    Chris Conover over 10 years
    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.
  • Christopher Pickslay
    Christopher Pickslay over 10 years
    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.
  • jailani
    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
    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
    Imran almost 9 years
    @bbum..Is this what u mean? stackoverflow.com/questions/4837565/… If not please give an example.
  • someUser
    someUser over 8 years
    Java does not support extension methods
  • Add080bbA
    Add080bbA over 8 years
    sorry ! Copy-paste typo. ios category similiar to c# class extension .
  • Fawkes
    Fawkes over 8 years
    "that must be declared in the main implementation block." -> that must be "implemented" in the main implementation block
  • Julie Yu
    Julie Yu over 8 years
    Thanks, I've modified it.
  • mfaani
    mfaani over 5 years
    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
    mfaani over 5 years
    Is this the answer to my question?
  • bbum
    bbum about 5 years
    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.
  • Pedro Trujillo
    Pedro Trujillo almost 5 years
    Can you add an example about ?