OC, oc Language
I. block
1> basic usage
- It is equivalent to a code block used to store code.
- High Efficiency
- Parentheses can be omitted if no parameters exist.
2> similarities between block and Function
- Code can be saved
- Possible return values
- The parameter can be set.
- Same call Method
3> block access to external variables
- Attackers can access external variables.
- By default, external variables cannot be modified within the block.
- Modify the external variable with the keyword _ block.
4> Use typedef to define the block type and Function Type
5> example
/* 1. use typedef to define a block type and a pointer to the function. define a summation function and point it to the function pointer. define a summation block with the new block type. define an output horizontal block 5. use the function pointer and block to sum two numbers respectively, and output a horizontal line in the middle */# import <Foundation/Foundation. h> // use typedef to define a return value type as int. There are two int type parameters: block type typedef int (^ myBlock) (int, int ); // use typedef to define a return value type as int. The function pointer typedef int (* sumP) (int, int) has two int type parameters ); // define a sum function int sum (int a, int B) {return a + B;} int main () {// use the new block type to define a summation block myBlock sumBlock = ^ (int a, int B) {return a + B ;}; // define a block void (^ lineBLock) () = ^ {NSLog (@"--------------");}; // point the function pointer to the sum function sumP p = sum; // outputs the sum of the two functions through block and NSLog (@ "% d", sumBlock (11, 12 )); // output a lineBLock () through the block; // outputs two numbers and NSLog (@ "% d", sumBlock (11, 12) through the function pointer )); return 0 ;}
Ii. protocol
1> basic usage
2> agreement compliance
3> @ property parameters
4> protocols and classifications
5> Protocol Application
6> example
/***** Main. m file *****/# import <Foundation/Foundation. h> # import "Parent. h "# import" Children. h "# import" Nanny. h "int main () {@ autoreleasepool {// define a Parent object Parent * p = [Parent alloc] init]; // define a Children object Children * c = [[Children alloc] init]; // define a Nanny object Nanny * n = [[Nanny alloc] init]; // initialize the property Children of object p to c p. children = c; // initialize the property nanny of object p to n p. nanny = n; // call the takeCareTheChildren method of object p [p takeCareTheChildren];} return 0;}/****** Parent. h file ******/# import <Foundation/Foundation. h> // # import "LookAfterTheChildren. h "// # import" Children. h "# import" Nanny. h "@ interface Parent: NSObject // declare the member variable nanny, and only accept objects that comply with the LookAfterTheChildren Protocol @ property (nonatomic, strong) id <LookAfterTheChildren> nanny; // declare the member variable children @ property (nonatomic, strong) Children * children; // define the method to implement the function of taking care of children-(void) takeCareTheChildren; @ end/****** Parent. m file *****/# import "Parent. h "@ implementation Parent //-(void) takeCareTheChildren {// delegate the nanny proxy to complete the task of taking care of children [_ nanny feedTheChildren]; [_ nanny playWithTheChildren];} @ end/***** Children. h file ******/# import <Foundation/Foundation. h> @ interface Children: NSObject @ end/***** Children. m file *****/# import "Children. h "@ implementation Children @ end/****** Nanny. h file ******/# import <Foundation/Foundation. h> // # import "LookAfterTheChildren. h "// to become a Parent proxy, follow the LookAfterTheChildren Protocol @ interface Nanny: NSObject <LookAfterTheChildren> @ end/****** Nanny. m file *****/# import "Nanny. h "@ implementation Nanny // implements the LookAfterTheChildren protocol and has the ability to take care of children-(void) feedTheChildren {NSLog (@" ");}-(void) playWithTheChildren {NSLog (@ "play with children");} @ end/****** LookAfterTheChildren. h file ******/# import <Foundation/Foundation. h> // The protocol defined by Parent declares the list of methods to take care of children @ protocol LookAfterTheChildren <NSObject> // The method for feeding children-(void) feedTheChildren; // Method for playing with children-(void) playWithTheChildren; @ end