Difference between category and class extension?

What is the difference between category and class extension. I believe both are used to add custom methods to existing classes. Can anyone shed some light on this? The exam with the code will be really appreciated.

+82
objective-c
Aug 17 '10 at 6:02
source share
9 answers

A category is a way to add methods to existing classes. They are usually found in files called "Class + CategoryName.h", for example, "NSView + CustomAdditions.h" (and .m, of course).

A class extension is a category, with the exception of two main differences:

  • The category has no name. It is declared as follows:

      @interface SomeClass ()
    
    
    - (void) anAdditionalMethod; 
    @end
  • The extension implementation should be in the main block of the @implementation file.

It is generally accepted to see the class extension at the top of the .m file, declaring more methods in the class, which are then implemented below in the main section of the @implementation of the class. This is a way to declare "pseudo-private" methods (pseudo-private in the sense that they are not private, not external). A.

+99
Aug 17 '10 at 6:36
source share
β€” -
  1. category

    => In Objective-C, when you want to add more functionality to a class without inheritance, you simply use a category for it.

    => it comes with its own .h and .m file

    => Category used to add a new method, not a property.

  2. Class extension

    β†’ In Objective-C, when you want to make the behavior of a property private, you use the class extension.

    β†’ It only comes with an .h file.

    β†’ mainly for properties.

Note: when we add a new file and select the target category parameter, it shows the category and β€œcategory on” and not β€œsubclass”, so it looks like

 @interface className (categoryName) @end 

-You will receive two .h and .m files with the file name as ( className + categoryName.h and className + categoryName.m )

and in case of expansion you will receive

 @interface className() @end 

-You will only get one file named className_extensionName.h

  • In the category you are not the owner of the class, but in the extension you are.
+43
Jul 04 '14 at 7:33
source share
  1. A category is a way to add methods to a class regardless of whether the source code is available, i.e. you can add a category to base classes such as NSString as well as to your own custom classes.

    An extension can only be added to classes whose source code is available, since the compiler compiles the source code and the extension at the same time.

  2. We can add additional instance variables and properties to the class extension, but not to the category.

  3. Any variable and method inside the extension is not even accessible to inherited classes.

  4. The category and extension are mainly designed to handle a large code base, but category is a way to extend the class API in several source files, while extension is a way to add the necessary methods outside the main interface file.

  5. Use a category when you need to split the same class code into different source files according to different functionality, and an extension when you just need to add some necessary methods to an existing class outside the main interface file. Also, when you need to change a publicly declared instance variable in a class, for example, read-only, to overwrite, you can re-declare it in the extension.

+30
Feb 20 '16 at 17:23
source share

We can also have properties. Using the set property associated with the 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/

+3
Jun 26 '15 at 7:51
source share
 @interface SomeClass () - (void) anAdditionalMethod; @end 

I think this is not a way to declare a category. Category must have a name

 @interface SomeClass (XYZ) - (void) anAdditionalMethod; @end 

eg

 @interface NSMutableArray (NSMutableArrayCreation) + (id)arrayWithCapacity:(NSUInteger)numItems; - (id)initWithCapacity:(NSUInteger)numItems; @end 

Announced for Apple's NSMutableArray

+2
Dec 13 '12 at 8:23
source share

Extension : to make methods private and add properties of our own custom class, not the Apple class.

Category : To add more methods to an existing class, rather than to a property, it can be used for both a custom class and an Apple class such as NSString .

+2
Oct 13 '17 at 21:01
source share

C # -like ios extension, abstract Java class, or interface

ios is like C #, a Java class extension

+1
Dec 01 '15 at 20:53 on
source share

Category

Categories are used when you create a file containing a large number of methods. Thus, they provide you with the ability to split one class into different modules. Also, if any changes are made to categories, the compiler does not waste time compiling the entire project. Frames cannot add a new variable or property and look at their parent class. You can override a method in a category, but this is not a good idea because this method cannot be overridden. Also, a thread can be implemented because all categories have the same hierarchical level and, therefore, two categories belonging to the same parent class can exist at run time. Protected methods can also be created using categories.

Extensions

Extensions allow you to override a property or add a new property to an existing parent class. Synchronously with categories, they have no name and are represented as @interface class () No .m file is present, and the method declared in the extension must be implemented in the @implementation of the parent file

Additional help at this link

0
Jul 24 '15 at 7:08
source share

Here is my understanding:

Extensions are commonly used to add additional features to our own custom class . We can add private methods or properties that extend the class interface that can be used in the implementation of the class.

Extensions must be written in the same file as the class. Therefore, you cannot write extensions for predefined types such as String, Float, etc.

On the other hand, categories can be used to add additional methods to pre-existing classes. For example, we can create custom methods by extending the String class. Please note that it cannot create additional properties in categories. In addition, the main advantage of categories is that we can write categories in any other file, outside the file in which your class terminates.

When creating categories, you must include the name in parentheses. But the extension does not require a name. Therefore, several times they are also called anonymous categories.

0
Oct 08 '17 at 12:41 on
source share



All Articles