Class extension vs class category
Asked Answered
S

4

91

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?

Socket answered 21/8, 2011 at 3:23 Comment(2)
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.Rosenthal
For extensions as they are used in Swift, see this question: #24143329Perfect
P
97

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.

Probable answered 21/8, 2011 at 3:26 Comment(13)
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.Probable
You can also add additional storage is class extensions using properties.Amish
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).Campy
Why Google is using category to implement private methods instead of extension? e.g. code.google.com/p/gdata-objectivec-client/source/browse/trunk/…Loco
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 extensionImaginative
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.Frustration
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.Gaillard
@chrisco what you mean? 1. Can't I write a extension for system class ex NSObject(I don't have source code for nsobject)?Tetrastichous
@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...".Frustration
@bbum..Is this what u mean? #4838065 If not please give an example.Gullible
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?Villainage
Is this the answer to my question?Villainage
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.Campy
R
33

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.

Rigsby answered 29/2, 2016 at 21:1 Comment(0)
D
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.

Danby answered 19/4, 2015 at 1:48 Comment(0)
P
-1

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

Pennon answered 1/12, 2015 at 20:47 Comment(3)
Java does not support extension methodsRecapitulation
sorry ! Copy-paste typo. ios category similiar to c# class extension .Pennon
Can you add an example about ?Reface

© 2022 - 2024 — McMap. All rights reserved.