Block explanation of iOS

Source: Internet
Author: User

A block, called a code block, is a C-level syntax and an attribute of the runtime, similar to a function (function pointer) in standard C, but its operation requires compiler and runtime support, and blocks are well supported from ios4.0.

Block is much like an anonymous method, but in fact it is a type that is used to encapsulate the saved code, as long as the block-encapsulated code can be executed at any time. Block is efficient, not only as a function parameter or a function's return value, but it can also take input parameters or return values.

Apple's official proposal to use block as much as possible. In multi-threaded, asynchronous tasks, collection traversal, collection sorting, animation transitions with a lot.

First, block definition

return value (^ block name) (parameter 1, parameter 2 ...) );

code

 //  block  void  (^myblocks1) ();  //  block  void  (^MYBLOCKS2) ();  //  nsstring style with the parameter void  (^MYBLOCKS3) (nsstring *);  //  multiple arguments, block  BOOL with a return value (^ MYBLOCKS4) (int , 

Second, block implementation

^ (parameter 1, parameter 2 ...) {Operation Body};

Code

MYBLOCKS1 = ^{NSLog (@"MyBlocks1");        }; MyBlocks2= ^(intnum) {NSLog (@"MyBlocks2, parameter is%i", num);        }; MYBLOCKS3= ^ (NSString *str) {NSLog (@"MyBlocks3, parameter is%@", str);        }; MYBLOCKS4= ^(intNUM1,intnum2) {NSLog (@"MYBLOCKS4"); if(num1>num2)returnYES; ElsereturnNO;};

  Third, the declaration of the simultaneous realization

Code

int (^MYBLOCKS5) (intint) = ^ (intint  num2) {        return num1>num2?  num1:num2;};

 Iv. global variables and local variables in code blocks

You can use a global variable in a block, or you can use a local variable, but an instance variable declared outside the block is used by default in a block, but cannot be modified. If you want to modify, you need to add __block before the variable

Code

 int  num1 = 10  ; __block  int  num2 = 10  ;  void  (^MYBLOCKS6) () = ^ () {NSLog ( @ " num1=%i, num2=%i   "        ,num1,num2);  // NUM1 + = ten;  //  error!!!     num2 + = 10  ; };

V. Call of the BLOCK

Block Name (argument 1, argument 2 ...);

Code

int number = MYBLOCKS5 (ten); myBlocks6 ();

Six, block as a parameter

Code

//declaration of the method+ (void) Dowmloadimg: (NSString *) URL success: (void(^) (NSString *pic)) Success Failure: (void(^) (NSString *error)) Failure;//implementation of the method+ (void) Dowmloadimg: (NSString *) URL success: (void(^) (NSString *pic)) Success Failure: (void(^) (NSString *error)) Failure {BOOL flag=NO; if(flag) {success ([NSString stringWithFormat:@"This is a picture%@", url]); } Else{failure (@"Download Failed"); }}//invocation of the method[Test dowmloadimg:@"Http://www.jerehedu.com/images/temp/logo.gif"success:^ (NSString *pic) {NSLog (@"%@", pic); } Failure:^ (NSString *error) {NSLog (@"%@", error); }];

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

Block explanation of 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.