Category-by using categories, we can dynamically add new methods to existing classes, and modularize the class definitions without being divided into multiple related files. Generally, a method is defined only in a category.
Class, interface definition, usually named as an existing "class + category name. H "@ interface existing class (category name )//....... method implementation @ end
For example, if you want to add a category to nsnumber, the interface code is as follows:
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(209, 47, 27); "><span style="font-size:10px;"><span style="color: rgb(120, 73, 42); ">//"NSNumber+code.h"</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(209, 47, 27); "><span style="font-size:10px;"><span style="color: rgb(120, 73, 42); ">#import </span><Foundation/Foundation.h></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;"><span style="color: #bb2ca2">@interface</span> NSNumber (code)</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;"></span></p><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); ">-(</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); color: rgb(112, 61, 170); ">NSNumber</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); "> *) add:(</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); color: rgb(187, 44, 162); ">double</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); ">) num;</span>
<span style="font-family:Menlo;font-size:10px;"></span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); ">-(</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); color: rgb(112, 61, 170); ">NSNumber</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); "> *) mult:(</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); color: rgb(187, 44, 162); ">double</span><span style="font-family: Menlo;font-size:10px; white-space: pre; background-color: rgb(240, 240, 240); ">) num;</span><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(187, 44, 162); "><span style="font-size:10px;">@end</span></p><div></div>
The implementation of this class is as follows:
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(209, 47, 27); "><span style="font-size:10px;"><span style="color: rgb(120, 73, 42); ">//"NSNuber+code.m"</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(209, 47, 27); "><span style="font-size:10px;"><span style="color: rgb(120, 73, 42); ">#import </span>"NSNumber+code.h"</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;"><span style="color: #bb2ca2">@implementation</span> NSNumber (code)</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">-(<span style="color: #703daa">NSNumber</span> *) add:(<span style="color: #bb2ca2">double</span>) num</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">{</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(61, 29, 129); "><span style="font-size:10px;"><span style="color: #000000"> </span><span style="color: #bb2ca2">return</span><span style="color: #000000"> [</span><span style="color: #703daa">NSNumber</span><span style="color: #000000"> </span>numberWithDouble<span style="color: #000000">:([</span><span style="color: #bb2ca2">self</span><span style="color: #000000"> </span>doubleValue<span style="color: #000000">] +num)];</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">}</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">-(<span style="color: #703daa">NSNumber</span> *) mult:(<span style="color: #bb2ca2">double</span>) num</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">{</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(61, 29, 129); "><span style="font-size:10px;"><span style="color: #000000"> </span><span style="color: #bb2ca2">return</span><span style="color: #000000"> [</span><span style="color: #703daa">NSNumber</span><span style="color: #000000"> </span>numberWithDouble<span style="color: #000000">:([</span><span style="color: #bb2ca2">self</span><span style="color: #000000"> </span>doubleValue<span style="color: #000000">] +num)];</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; "><span style="font-size:10px;">}</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(187, 44, 162); "><span style="font-size:10px;">@end</span></p>
All instances of the nsnumber class will have the Add:, multi: method to achieve dynamic expansion of the nsnumber class.
We know that if a function is defined in the. h file without declaration, this method is a private method and can only be used in files of this class. If we declare this method in the category excuse section, we can access this private method.
-----------------------------------------------------------------------------
Extension)
The extension is similar to the category. The extension is equivalent to an anonymous category, that is, there is no category name.
Class, interface definition, usually named as an existing "class _ XXX. H" @ interface existing class () // ...... method implementation @ end
In terms of usage, categories usually have separate. h and. M files. Extension is temporary extension of a class. The class implementation part simultaneously implements the methods defined in the class Interface part and the methods defined in the extension. When defining class extensions, you can add additional instance variables or use the @ property and @ synthesize keywords. However, you cannot define additional instance variables when defining the additional list of classes.
There is a class named vehicle, and only the name attribute does not have a defined method. Extend it
#import "vehicle.h"@interface Vehicle ()@property(nonatomic, strong) NSString *color;-(void) drive:(NSString *)name;@end
Add the color attribute and drive: Method for the original vehicle class. Therefore, in the vehicle. m implementation file, implement the method declared in vehicle. h and the drive: method.
//"Vehicle.m"
# Import "vehicle_ext.h" @ implementation vehicle-(void) Drive :( nsstring *) name {nslog (@ "Vehicle name % @, color attribute % @", _ name, _ color) ;}@ end
In practice, the common practice is to omit the vehicle_ext.h file, but add the content directly to the front of the vehicle. M file @ implementation.
-----------------------------------------------------------------------------
Protocol)
Based on nsobject, a category is created for nsoject. When a category is created, a new method for this category is specified to implement informal protocols. For classes that implement informal protocols in OC, OC does not enforce all methods in the Protocol.
Define the official protocol using the @ protocol keyword
@ Protocol name <parent Protocol 1, parent Protocol 2> {// ...... method declaration} @ end
Protocols support multi-inheritance. The @ required \ @ optional keyword is added to the oc2.0 standard. The declaration method must be implemented or optional. The default value is @ required.
The protocol-compliant syntax is as follows:
@ Interface Class Name: parent type <Protocol 1, Protocol 2...>
If the protocol is used to define variables, these variables can only call the methods declared in the protocol, otherwise the compiler will prompt an error.
Two syntaxes for defining variables using protocols
Nsobject <Protocol 1, Protocol 2...> * variable name; id <Protocol 1, Protocol 2...> variable;
The Protocol represents a specification. The Protocol class can delegate the method defined by the Protocol to the class implementing the Protocol, so that the class can be more universal, specific actions are completed by the Implementation class of the Protocol. In the development of a large number of applications, the concept of dependency delegation is large.
Categories, extensions, protocols and delegation in oC