The Block app in iOS

Source: Internet
Author: User

In this big winter, silently knocking on the keyboard, do not spray. Learning Swift today, learning about closures, discovering closures and OC blocks has a lot of similarities, re-learning and learning some of the advanced points of usage, as follows:

1.block Format Description: (return type) (^ block name) (parameter type) = ^ (parameter list) {Code Implementation};//if there are no arguments, the () of the argument list after the equals sign can omit the example:

void (^demoblock) () = ^ {

NSLog (@ "demo Block");

};

Int (^sumblock) (int, int) = ^ (int x, int y) {

return x + y;

};

The variables used in 2.block are retained as duplicates, and the copied variables of the block are preserved in the block, and by default, variables outside the block are read-only in the block! When you use the __block keyword, you can also modify the value of the external variable in the block. 3. A typedef can be used to define the type of block, which is convenient for subsequent direct use, such as:

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 block, the TypeDef keyword is not frequently used in real-world development because the block is highly flexible, especially when passed as a parameter. The purpose of using block is to immediately declare the following using the official array traversal method:

-(void) Enumerateobjectsusingblock: (void (^) (id obj, Nsuinteger idx, BOOL *stop))block;

If you use TypeDef, you need: (1) typedef void (^Enumerateblock) (ID obj, Nsuinteger idx, BOOL *stop);(2)-(void) Enumerateobjectsusingblock: (enumerateblock) block;

The end result is that Enumerateblock has no other use other than defining the type.

5.Block can be passed directly as a parameter

Nsarray *array = @[@ "Zhang San", @ "John Doe", @ "Harry", @ "Zhao Liu"];

[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {

NSLog (@ "%d content is%@", (int) idx, obj);

if ([@ "Harry" 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 multiplied equals:%f", Block (Number,withnumber));

}

[Self add:4 withnumber:2 sumblock:sumblock];

6. Since block is a data type, block can be added to the array as a special object

#pragma mark defined and added to an 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. Remove circular references

Local variables are strongly referenced by default and are freed after they leave their scope using the __weak keyword, you can declare a local variable as a weak reference

__weak demoobj *weakself = self;

N References weakself in block, the block no longer makes a strong reference to self

Int (^sum) (int, int) = ^ (int x, int y) {

return [weakself sum:x y:y];

};

8. Today's focus, referring to the framework of AFN, blocks call success or failure function arguments according to the function, and pass in the parameters as follows:

Because the main still used to judge success and failure functions, so look at the uncomfortable classmate can self-repair, will success replaced by add, will failure replaced multiplied

-(void) Viewdidload {
[Super Viewdidload];
Self.isbool = true;//is added when Isbool equals true, or multiplied

[Self setcompletionblockwithsuccess:^ (int x,int y) {
NSLog (@ "Add equals:%d", x+y);

NSLog (@ "Success");


} failure:^ (int x,int y) {

NSLog (@ "Multiplies equals:%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) {//Determine which parameter is passed in, execute success or failure
NSLog (@ "start adding");
Success (SELF.NUMBER2,SELF.NUMBER3);//parameter passed in setcompletionblockwithsuccess
}else{
NSLog (@ "Start multiplying");
Failure (SELF.NUMBER2,SELF.NUMBER3);//parameter passed in setcompletionblockwithsuccess
}
};


}

The Block app in iOS

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.