OC category Extension (extension) Agreement (Protocol) (excerpt)

Source: Internet
Author: User

The objective-c provides the means by which we can extend class definitions: classes, extensions, and protocols. Category: Add new methods to known classes; extensions: Notifications use classes to declare private methods in the definition of this class; Protocol: A protocol declares a method that can be implemented by any class. Note: These methods can only increase the method of the class, and can not be used to increase the instance variable, to increase the class instance variable, can only be implemented indirectly by defining subclasses. 1, Category 1) Declaration category @interface NSString (numberconvenience)-(NSNumber *) Lengthasnumber; @end the declaration has 2 characteristics. First, the existing class is behind the @interface keyword, followed by a new name in parentheses. The declaration indicates that the name of the category is Numberconvenience, and that the category adds a method to the NSString class. As long as you guarantee the uniqueness of the category name, you can add any number of categories to a class. Second, you can specify the class to which you want to add a category and the name of the category, and you can also list the methods that you added, and finally end with @end. Because the new implementation variable cannot be added, unlike the class declaration, there is no instance variable part in the declaration of the category. 2) class Purpose limitations First, you cannot add a new instance variable to a class. The category has no location to accommodate the instance variable. Second, the name conflict, that is, the method in the category is the same as the existing method. When a name conflict occurs, the category has a higher priority. You get the class method will completely replace the initial method so that the initial method can no longer be used. Some programmers add a prefix to their class methods to ensure that no name collisions occur. There are some techniques that can overcome the limitations of categories that cannot add new instance variables. For example, you can use a global dictionary to store the mappings between objects and the extra variables that you want to associate. But at this point you might want to think about whether a category is the best choice for accomplishing your current tasks. 3) class purpose function Cocoa category is mainly used for 3 purposes: first, the implementation of the class is dispersed into different files or different frameworks. Second, create a forward reference to the private method. Third, add an informal agreement to the object. 2, extension of the extension of the class can be regarded as an anonymous class, the class sometimes need to be seen only for themselves, the private method used by the private method can be declared in an extended manner, the extension of the method defined in the class itself in the @implementation code area of the implementation. Definition extension @interface myobject:nsobject {    nsnumber *number;}  -(NSNumber *) number; @end    @interface MYobject (Setter)  -(void) Setnumber: (NSNumber *) newnumber; @end   @implementation myobject -(NSNumber *) number{     return number;} -(void) Setnumber: (NSNumber *) newnumber{//do something} @end when a class name is not provided when the extension is defined, the method defined in the extension is considered to be a "must implement" API in this case, If the method lacks implementation code, the compiler will report a warning, at which point the implementation of the method must appear in the @implementation code block of the body of the class. 3, the Protocol and Proxy mode protocol only declares the method, does not implement specifically, accepts the agreement the object is responsible for realizes. The OC protocol is a list of methods that are declared by @protocol, requiring other classes to be implemented, equivalent to the declarations of the @interface section. Note: A. The method of @required adornment in the protocol should be implemented when the agreement is confirmed B.selective implementation of @optional adornmentsMethod C. Use [Object Conformstoprotocol: @protocol (Protocol)] to determine whether to follow the Protocol D. The protocol is written in the. h file that provides the Protocol class. The proxy mode is the thing that does not do the time itself, but requires others to do it.

OC category Extension (extension) Agreement (Protocol) (excerpt)

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.