////main.m//Block Basic Use////Created by Ymmmsick on 15/7/21.//Copyright (c) 2015 Ymmmsick. All rights reserved.//#import<Foundation/Foundation.h>typedefint(^Intblock) (); typedefvoid(^Voidblock) ();intMainintargcConst Char*argv[]) {@autoreleasepool {//Insert code here ...NSLog (@"the modified template of this elder brother!"); //block without a row parameter and no return value void(^testblock) () = ^{NSLog (@"Block---->test"); NSLog (@"Block---->test"); }; Testblock (); //block with a return value for a row parameter int(^sumblock) (int,int) = ^(intAintb) { returnA +b; }; NSLog (@"sum is:%d", Sumblock (Ten, A)); //typedefIntblock Productblock = ^ (intAintb) { returnAb; }; NSLog (@"product is:%d", Productblock (Ten,Ten)); /*block access external variable 1.block internal can access the external variable 2. By default, the block cannot modify the external variable 3. External variable add __block keyword, block inside can be repaired Change the external variables*/__blockintA =0; Voidblock Areablock= ^{a=Ten; NSLog (@"A =%d", a); }; Areablock (); } return 0;}
OC Block Basic use