IOS category and extension, Delegate's understanding

Source: Internet
Author: User

The difference and usage of catrgory and extension and delegate

Apple's official documentationcategory is used very frequently in iOS development. Especially in the expansion of the system class, we can not inherit the system class, directly to the system class to add methods, the maximum extent of the objective-c dynamic language characteristics.

#import

@interface NSObject (Category)

-(void) MyMethod;

@end

This is the simplest categoryto use for the NSObject class and adds a method to NSObject.

Points to note in using Category:

(1) The method of category does not have to be implemented in @implementation, but it can be implemented in other locations, but when the method of category is called, the implementation of the method is not found according to the inheritance tree, and the program crashes.

(2) Category theory cannot add variables, but you can use @dynamic to compensate for this deficiency.

#import

static  const  void  * Externvariablekey  =&externvariablekey ;

@implementation nsobject (Category)

@dynamic variable;

-(ID) variable

{

           return  objc_getassociatedobject ( Span style= "word-wrap:normal; Word-break:normal ">self ,  externvariablekey );

}

-(void) setVariable:(ID) Variable

{

objc_setassociatedobject ( Self, Externvariablekey, variable, objc_association_retain_nonatomic);

}

-----------------------------------------------------------------------------------------

Extension is very much like a category that is not named.

@interface Myclass:nsobject

@property (retain, readonly) float value;

@end

In general, extension are placed above the @implementation in the. m file.

@interface MyClass ()

@property (retain, readwrite) float value;

@end

Use Extension points to note:

(1) The method in extension must be implemented in the @implementation, otherwise the compilation will error.



Protocol (PROTOCOL) does not have a parent class and cannot define an instance variable, only declares a method, declares an interface of an unknown class, communicates between two classes

@required: Must be implemented. Default

@optional: Selective implementation

NSObject is a basic class root class is also a base protocol, can write or do not write

Proxy design Pattern: Two objects work together to solve the problem, for communication between objects

Typically used for communication between control and Controller objects, control events occur through the controller behavior

This means that the object class has a delegate method, the implementation of the delegate method (adhering to the delegation protocol) needs to be implemented in other classes, and other class objects to resolve the


IOS category and extension, Delegate's understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.