简体   繁体   中英

Class Extension vs Primary Interface vs Category

I am fairly new to realm of iOS. Coming from Java and Android background i am facing few challenges while learning objective C.

My question: I understand how the above three are different from each other but I fail to understand their use cases in practice.

Do we need a Class Extension for every class with private functions? What is the use of Category, when we can extend a Cocoa/Cocoa-touch class in interface and add custom functions? Please do provide some example from your experience.

  • Categories are a way to split a single class definition into multiple files.A category can be declared for any class, even if you don't have the original implementation source code.At runtime, there's no difference between a method added by a category and one that is implemented by the original class.

example for categories:

     NSString+UrlString.h
     #import <UIKit/UIKit.h>
     @interface NSString(Additions)

     +(void)urlMethod;
     @end 

      NSString+UrlString.m//implmentation
      #import "NSString+Additions.h"

      @implementation NSString (Additions)

      +(void)urlMethod
      {
      }

      @end
  • The primary interface for a class is used to define the way that other classes are expected to interact with it. In other words, it's the public interface to the class.

  • 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.

    Class extensions are used to declare private methods in objective C

    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.

    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 extension uses the @interface keyword, just like a standard Objective-C

     @interface ClassName () @end 

    you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. Please refer this

  • Class Extesions : If you mean by Extension Methods like in .Net, then it called as Category in Objective-C.
  • Categories : These are nothing but the Extension Methods, it allows to add methods in existing classes from iOS SDK (like NSString, NSURL, etc.) For more details: Apple Doc: Category

  • Primary Interface : Writing a class (Interface in terms of Objective-C) definition inside its implementation file called primary interface.

     //ClassName.mm @interface ClassName() { Declarations; } - Methods; + Methods; @end @implementation ClassName @end 

    So, Categories are also one type of primary interfaces.

Category is adding methods to a class in the runtime. As far as the runtime is concerned, the methods that are implemented in a class extension, ARE the methods that are available for the class itself. Category in Objective-C is a fancy name for Monkey Patching in other programming languages like C#. You can read about it here .

With that said, you can create a category for UIColor with some method if you want every UIColor to have that behaviour throughout that module. This isn't the case with subclassing. Only the subclassed (theoretically speaking) UIColor object will get those behaviour since there is a distinct difference in the type of the object.

Example:

UIColor has built in methods that give you different colors; you can call UIColor.greenColor() to get the green color; UIColor.blackColor() to get black color and so on...

Suppose you want your own to be called in a similar fashion, you create a category (example in swift) like so

extension UIColor {
    static func yourColor() -> UIColor {
       return UIColor(red:220/225,green:222/225,blue:223/225)
    }
}

This way, it is valid for you to call UIColor.yourColor() . Every UIColor that you would use has this method available. Convenient than subclassing, isn't it?

Creating a subclass has polymorphic implications; categories don't. You subclass only when you need refinement of an existing class and treat it both as a parent and the child when required. As a Java developer you would know when it makes sense to subclass.

An extension is best for private methods which you would like to declare in your .m file. Think of extension as a category private to the .m file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM