-----------------------------------------------Category--------------------------------------
The class is to add the method on the basis of the original class, and only need to use the original class to instantiate the object, the call can be. You cannot add variables or attributes to a class, only add methods.
1. Under what circumstances is the actual development used?
A: Expand a class that is already encapsulated (you do not want to change the original class) in this case, you can only use the class
Use category when developing a module within a team
2. Do not overwrite the method of the original class, which causes the super message to break, because the methods in the class purpose have higher precedence.
3. What is the difference between class and inheritance?
Inheritance: You can extend the method of the parent class, you can add instance variables, properties.
Class: You can extend the methods of the original class, and you cannot add instance variables, properties.
4. How extension methods are invoked
Inheritance: Calls an extension method with the object of the subclass.
Class: The object invocation of the original class is still used.
5. New Object-c.file file will appear the original class + (the name of the extension you built);
Example: extending an array
Code:
Declare the method you need to extend in the. h file:
#import <Foundation/Foundation.h>
@interface Nsarray (ARR)
-(NSString *) Link;
@end
Implement the method you need to extend in the. m file:
#import "Nsarray+arr.h"
@implementation Nsarray (ARR)
-(NSString *) link{
nsmutablestring *str = [[nsmutablestring alloc]init];
for (ID-n-Self)
[Str appendstring:n];
return str;
}
@end
Called in the main file:
Nsarray *y = @[@ "I love China", @ "I love oc", @ "I love Mountains and Waters"];
NSLog (@ "%@", [y link]);
The result of the output is:
I love China I love oc I love mountains and waters
--------------------------------------------Extension---------------------------------------
The extension of a class is the addition of private methods, variables, and declarations in the class to the @interfance class name () and its corresponding @end in the class itself.
Classes sometimes require methods that are visible only to themselves, and then we can define the private methods of the class in an extended way
But the difference between this approach and the previously defined @private
The difference between the two is that @private is defined in the. h file, and an extension is defined in the. m file.
If it is open source, it is generally open. h file
If it is closed source, generally only open. m files
Note: The private method, which is defined only in the. m file, is not declared in the. h file, and is typically used in the class itself.
There are two ways of creating an extension:
1. New object-c.file-->extension, oo.h file appears
2. Directly in the. m file of the class, enter it on the @implementation
@interface See ()
@end
Example: (in the second way)
Create a new person class. In its. m file:
Add a private variable and method.
#import "Penson.h"
@interface Penson ()
{
NSString *name;
}
-(void) name;
@end
@implementation Penson
-(void) name{
Name = @ "People";
NSLog (@ "%@ you created successfully", name);
}
-(void) Impli: (NSString *) k{
NSLog (@ "%@", K);
[Self name];
}
@end
protol------------------------------Protocol-------------------------------------------
1. The protocol can be implemented by any class, and the protocol itself is not a class, it defines an interface that other classes can implement.
The protocol is only declaring that it is not implemented
(non-fulfilling categories are also called informal agreements.)
Because classes are generally inherited from NSObject
)
2. The informal agreement is expressed in parentheses (), and the formal agreement is a <> that the agreement is introduced with angle brackets
in which class to import the protocol in which class to implement this Protocol method
The default is the required mandatory implementation of the method, do not implement the error.
The @optional of the selected implementation can be implemented without implementation.
After you create a new protocol, only one. h file appears
@protocol xieyi <NSObject>
-(void) FANGFA;
@optional
-(void) fangfa1;
@end
After introducing xieyi.h into a class, this protocol is used.
-(void) Fangfa; This method must be implemented, or an error will be
and-(void) fangfa1; After adding @optional, you can choose to implement or not implement. Do not implement and will not error.
Note: If you want to introduce more than one protocol in a class, the number is separated by @interface person:nsobject<xixieyi,shangke,zixi,zongjie>
oc-category, extension, agreement