Category, protocol, delegate summary

Source: Internet
Author: User
Tags what interface multiple inheritance in c

I. Category (category) is a mechanism for extending a class to add new methods to existing classes. Basic syntax for the ① category @interface parts, such as:@interface NSString (numberconvenience)-(NSNumber *) Lengthasnumber; @end we added a category named Numberconvenience for the NSString class. Note:
    • Categories can only add methods, no instance variables can be added
    • Categories can add properties, but properties must be of type @dynamic
@implementation Section@implementation NSString (numberconvenience)-(NSNumber *) lengthasnumber{...} Benefits of the ② category
    • The implementation code of the class is dispersed across several different files and frameworks, which can be divided into multiple modules, reducing the complexity of the class and facilitating maintenance.
    • The extended functionality of the category, where existing classes can be extended by category without change.
    • Classes can implement localized encapsulation of functionality, simulating the implementation of private methods.
    • Add an informal agreement to an object by category. This allows objects of any class to be used as delegate objects, which list all the methods that an object can perform.
Disadvantages of the ③ category
    • Category cannot add instance variables
    • A name conflict occurs in a category where the methods in the category have the same names as the existing methods, and the categories have higher precedence.
Two. Protocol (protocol) is similar to pure virtual functions in C + +, the protocol is simply declared, not implemented, implements the basic syntax of the ① protocol in classes conforming to this Protocol
    • Claim Agreement
such as: @protocol nscopying-(ID) Copywithzone: (Nszone *) zone; @end
    • Adoption Protocol
The names of the protocols are listed in the @interface declaration of the class, such as: @interface car:nsobject <nscopying>{...} @end//car
    • @required and @optional properties (new features for OBJECTIVE-C 2.0)
@required property requires methods that conform to this protocol, @optional attributes are not mandatory, and can be judged by Respondstoselector: @select () ② Considerations
    • Formal agreement required to display the adoption
    • Agreement name must be unique
    • Adopting a protocol means that you commit to implementing all the methods of the @required property of the protocol. Otherwise, the compiler generates a warning to remind you.
    • Protocol can inherit parent protocol
such as: @protocol the meaning of Mysuperduberprotocol <myparentprotocol>③ agreement
    • Protocol can split the business method definition into a separate file, without concern for the function can not focus on his specific implementation, so as to facilitate the division of labor
Three. Delegate (delegate/proxy) a delegate is an object that specifies a design pattern for another object to handle certain tasks, such as: We let a object do a work, but the B object does not do the work itself, it has a proxy a object, a object will do the work. as follows://a.h@interface a:nsobject-(void) Log; @end//a.m@implementation a-(void) log{     nslog (@ "This is A Log");} @end            //b.h@interface b:nsobject{   a* delegate;} @property   (nonatomic, retain) * delegate;-(void) calllog; @end//b.m@implementation b@synthesize  delegate;-(ID) init{   if  (self = [super init]) {         delegate = [[A alloc]init];   }    returnself;} -(void) Calllog{    nslog (@ "This is B calllog");    [Self.delegate Log];} @end Four. Related Topics ①objective-c can multiple inheritance be implemented in a class? Can I implement multiple interfaces?     &NBSP;OBJECTIVE-C can not use multiple inheritance directly, all classes in cocoa are subclasses of NSObject, to implement multi-inheritance to use Protocol delegate agent. Multiple interfaces can be implemented and multiple inheritance in C + + can be achieved through multiple interfaces. What is the difference between  ② inheritance and categories in implementation?      
    • Categories can add new methods without changing the original code, but inheritance is not possible.
    • Categories can only add methods, cannot delete modify methods, but inheritance can be increased, or you can modify or delete methods.
    • Categories cannot increment properties, but inheritance can.
Differences between the ③ category and the anonymous category (class extension/extension)
    • Only add methods in categories
    • A class extension can increase not only the method, but also the instance variable, except that the instance variable defaults to the @private type (scoped only to its own class, not to subclasses or elsewhere);
    • The method declared in the class extension is not implemented and the compiler will alert you, but the methods in the category are not implemented by the compiler without any warning. This is because the class extension is added to the class during the compile phase, and the category is added to the class at run time.
    • A class extension is a very good way to declare a private method in a. m file.
    • The method declared by the class extension must be implemented by the implementation part of the corresponding class (that is, it cannot have a separate implementation part (@implementation part) as a category)
    • Class extensions can only invoke methods in this class, and subclasses cannot invoke the extension of the parent class.
The combination of ④delegate and protocol is a protocol that defines what kind of public interface to implement, and delegate is what interface the object of the delegate needs to implement. The combination of the two can be understood as the object being delegated needs to implement the public interface required by protocol. such as:-(ID <NSNetServiceBrowserDelegate>) delegate; requires the requested object to implement the interface required by the protocol nsnetservicebrowserdelegate, only need to comply with the required protocol, You can set any object as a delegate. ⑤ talk about the application of delegate, protocol and category in the actual development project protocol&delegate:In peacetime work, often appear need to develop an app, but the app itself does not care about the content of the business section, such as: There is a trading mode is the app-side drive terminal to trade (we call the app as the host computer), the specific transaction data collection data packaging by the terminal responsible, And the host computer is responsible for the server and the communication and UI display, that is, the host computer does not need to care about the end of the specific transaction implementation, the use of protocol custom set of interfaces, the app only need to call, specific implementation in the terminal part of the implementation. But at the same time, the host computer to send data to the server interface is called by the terminal, at this time you can use delegate implementation of the delegation, by the host computer to implement this interface. Category:For example, in a bottom-level library within a company package, a header file defines a class for storing Bluetooth information, which is read-only. Now you need to call this class, and want to rewrite some of the information, and add some initialization of the Bluetooth device interface, at this time you can not define a new similarity without changing the original class based on the class extension to achieve the requirements. Implementation of ⑥ callback function
    • Implemented through block
    • Implemented through protocol
    • Implemented through delegate
    • Combined with protocol and delegate implementations

Category, protocol, delegate summary

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.