Code blocks for iOS Learning (block)

Source: Internet
Author: User

Code blocks (block)

(1) The main role : Save a piece of code, where needed to call.

(2) The use of global variables in code blocks :

Global variables can be used in blocks of code and can also be changed, with the following code snippet:

1 intLocal =1;//NOTE: Global variables2 void(^BLOCK0) (void) = ^(void){3Local + +;4NSLog (@"local =%d", local);5 }; 6 block0 ();7NSLog (@"external local =%d", local);

The result is: local = 2;

External local = 2;

(3) The use of local variables in code blocks :

* * General local variables can only be used in code blocks, but cannot be changed (cannot be compiled at this time), the code snippet is as follows:

1 intn = +;//local variable2 void(^BLOCK1) (void) = ^(void){3 //n++;4NSLog (@"FLOAT1 =%d", n);5 };6 Block1 ();7NSLog (@"Float2 =%d", n);

The result is: FLOAT1 = 1000

FLOAT2 = 1000

* * When a local variable is declared as __block, it can be used not only in the code block, but also can be changed. The code snippet is as follows:

1__blockintj = $;//declared as __block variable2 void(^BLOCK2) (void) = ^(void){3J + +;4NSLog (@"block variable =%d", j);5 };6 Block2 ();7NSLog (@"Fast Variable 2 =%d", j);

The result is: block variable = 201

Fast Variable 2 = 201

(4) code blocks are used as method parameters :

Custom classes:

First BlockClasss.h class:

1 #import<Foundation/Foundation.h>2 3 @interfaceBlockclasss:nsobject4 5 //declaring an int type variable6@property (nonatomic,assign)intresult;7@property (nonatomic,assign)intresult2;8 9- (int) Result: (int(^) (int) ) block;Ten  One-(BLOCKCLASSS * (^) (intvalue)) add; A @end

Then the implementation of BLOCKCLASSS.M:

1 #import "BlockClasss.h"2 3 @implementationblockclasss4- (int) Result: (int(^) (int)) block{5_result =block (_result);6     return_result;7 }8 9-(BLOCKCLASSS * (^) (int)) add{Ten     return^BLOCKCLASSS * (intvalue) { One_RESULT2 + =value; A         returnSelf ; -     }; - } the @end

Code blocks are used as code for method parameters:

1 blockclasss *bl = [[blockclasss alloc]init]; 2 [bl result:^int(int  result) {3     ; 4     2 ; 5     return   result; 6 }]; 7 NSLog (@ "result =%d", Bl.result);

The result is: result = 20

(5) The code block is used as a method return value:

1 blockclasss *bl2 = [[blockclasss alloc]init]; 2 bl2.add (+). Add (+); 3 NSLog (@ " result ^=%d", BL2.RESULT2);

Print Result: Result ^=300

(6) Avoid circular references :

You cannot use self or _xxx in a block of code in order to prevent circular references from occurring. It is best to define self externally as the __weak property and then to use it.

For example:

1 __weak self weakself = self ; 2 _block = ^{3    NSLog (weakself.result); 4 };

Code blocks for iOS Learning (block)

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.