iOS Design--oc Learning Summary Extension category agreement

Source: Internet
Author: User

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. Class items
1) Declaration of 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 the 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 effect
The 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
The extension of a class can be thought of as an anonymous class, and the class sometimes needs to be seen only for itself, and the private method used by the private method can be declared in an extended manner, and the method defined in the extension is implemented in the @implementation code area of the class itself.
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 both "must implement" APIs in this case, if the method lacks the 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 class body.
3. Protocol and Proxy mode
The protocol only declares the method and does not implement it, and the object accepting the agreement is responsible for implementing it. 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.
Attention:
A. The method of @required modification in the protocol should be implemented when the agreement is confirmed
B. Methods that can selectively implement @optional adornments
C. Use [Object Conformstoprotocol: @protocol (Protocol)] to determine compliance with the Protocol
D. The agreement is written in the. h file that provides the Protocol class.
Application of protocol--agent
The proxy pattern is that it does not do the time itself, but rather requires others to do it.

iOS Design--oc Learning Summary of Extended-class protocol

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.