Block applications in ios and iosblock applications
In this winter, I have quietly typed on the keyboard. today, when I learned about swift, I learned the closure and found that there are many similarities between the closure and the oc block. I re-learned it and learned some advanced usage points. The content is as follows:
1. block format description: (return type) (^ block name) (parameter type) = ^ (parameter list) {Code Implementation}; // if no parameter exists, the parameter list following the equal sign () examples can be omitted:
Void (^ demoBlock) () = ^ {
NSLog (@ "demo Block ");
};
Int (^ sumBlock) (int, int) = ^ (int x, int y ){
Return x + y;
};
2. the variables used in the block will be retained in the form of replication, and the copied variables of the block will be retained in the block. By default, the external variables of the Block are read-only in the Block! When the _ block keyword is used, you can also modify the value of the external variable in the Block. 3. You can use typedef to define the type of a Block for future use. For example:
Typedef double (^ MyBlock) (double, double );
MyBlock area = ^ (double x, double y ){
Return x * y;
};
MyBlock sum = ^ (double a, double B ){
Return a + B;
};
NSLog (@ "%. 2f", area (10.0, 20.0 ));
NSLog (@ "%. 2f", sum (10.0, 20.0 ));
4. although typedef can simplify the definition of a Block, the typedef keyword is not frequently used in actual development. This is because the Block has great flexibility, especially when passing parameters, the purpose of using Block is to immediately use the official array Traversal method and declare it as follows:
-(Void) enumerateObjectsUsingBlock :( void (^) (id obj, NSUInteger idx, BOOL * stop) block;
If typedef is used, you need to: (1) typedef void (^ EnumerateBlock) (id obj, NSUInteger idx, BOOL * stop); (2)-(void) enumerateObjectsUsingBlock :( EnumerateBlock) block;
The final result is that, apart from defining types, EnumerateBlock is of no use.
5. The Block can be directly transmitted as a parameter.
NSArray * array = @ [@ "Zhang San", @ "Li Si", @ "Wang Wu", @ "Zhao Liu"];
[Array enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop ){
NSLog (@ "the content of item % d is % @", (int) idx, obj );
If ([@ "" isEqualToString: obj]) {
* Stop = YES;
}
}];
Or:
MyBlock sumBlock = ^ (double x, double y ){
Return x * y;
};
-(Void) add :( int) number withNumber :( int) withNumber sumBlock :( MyBlock) block
{
NSLog (@ "execute add :( int) number withNumber :( int) withNumber sumBlock :( void (^) (void) block ");
NSLog (@ "hahaha -- 4 and 2 are multiplied by: % f", block (number, withNumber ));
}
[Self add: 4 withNumber: 2 sumBlock: sumBlock];
6. Since Block is a data type, you can use it as a special object and add it to an array.
# Pragma mark definition and add to array
@ Property (nonatomic, strong) NSMutableArray * myBlocks;
Int (^ sum) (int, int) = ^ (int x, int y ){
Return [self sum: x y: y];
};
[Self. myBlocks addObject: sum];
Int (^ area) (int, int) = ^ (int x, int y ){
Return [self area: x y: y];
};
[Self. myBlocks addObject: area];
# Pragma mark calls the Block stored in the array
Int (^ func) (int, int) = self. myBlocks [index];
Return func (x, y );
7. Release circular references
By default, local variables are strongly referenced. After they are out of the scope, they will be released using the _ weak keyword. You can declare local variables as weak references.
_ Weak DemoObj * weakSelf = self;
N references weakSelf in the Block, so the Block will not make a strong reference to self.
Int (^ sum) (int, int) = ^ (int x, int y ){
Return [weakSelf sum: x y: y];
};
8. For more information, see the AFN framework. block calls the success or failure function parameters based on whether the function is correct or not, and input the following parameters:
// Because it is mainly used to judge success and failure functions, you can make up your mind to replace success with add and replace failure with multiplied.
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. isbool = true; // when isbool is equal to true, it is added. Otherwise, it is multiplied.
[Self setCompletionBlockWithSuccess: ^ (int x, int y ){
NSLog (@ "add equals: % d", x + y );
NSLog (@ "success ");
} Failure: ^ (int x, int y ){
NSLog (@ "Multiply =: % d", x * y );
NSLog (@ "failure ");
}];
}
-(Void) setCompletionBlock :( void (^) (void) block {
Block ();
NSLog (@ "setCompletionBlock number2: % d number3: % d", self. number2, self. number3 );
}
-(Void) setCompletionBlockWithSuccess :( void (^) (int x, int y) success
Failure :( void (^) (int x, int y) failure
{
Self. completionBlock = ^ {
Self. number2 = 10;
Self. number3 = 20;
If (self. isbool = true) {// determines which parameter is passed in and whether to execute success or failure
NSLog (@ "start adding ");
Success (self. number2, self. number3); // pass in setCompletionBlockWithSuccess
} Else {
NSLog (@ "Start multiplication ");
Failure (self. number2, self. number3); // pass in setCompletionBlockWithSuccess
}
};
}