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