first, what is blocksBlock is a C-level syntax and a feature of the runtime, similar to a function (function pointer) in standard C, but its operation requires compiler and runtime support, and is well-supported from ios4.0.
second, in iOS development, under what circumstances use blockIn addition to being able to define parameter lists, return types, blocks can also obtain state (such as local variables) within the lexical scope that is defined, and can modify these states under certain conditions, such as using the __block variable. In addition, these modifiable states are shared among multiple blocks within the same lexical scope, and can continue to share or modify these states, even if the lexical scope (such as stack expansion, out of scope) is out. In general, blocks are encapsulated in short snippets of code that are used as work units, often for concurrent tasks, traversal, and callbacks.
third, how to declare the block (in contrast to the C language function declaration) Four, C function and blocks callInt (*cfunc) (int a) function calls int result = Cfunc (10); Int (^bfunc) (int a) function calls int result = Bfunc (10);
Five, __block key wordsThe inside of a block can refer to its own extraterritorial variables, including static variables, extern variables or free variables (when defining a variable, if no modifier is stored, by default the free variable Auto,auto variable is saved in the stack. In addition to auto, there are storage modifiers such as register,static, which are read-only in the block for free variables. In addition to the introduction of block, a special __block keyword variable storage modifier is introduced.
Vi. Several small examples of block
Java code
- #Import <Cocoa/Cocoa.h>
- int main (int argc, char *argv[])
- {
- @autoreleasepool {
- NSLog (@"Hello World");
- void (^myblocks) (void) = NULL;
- Myblocks = ^ (void) {
- NSLog (@"in blocks");
- };
- NSLog (@"before Myblocks");
- Myblocks ();
- NSLog (@"after Myblocks");
- Int (^MYBLOCKS2) (int A, int b) = ^ (int A, int b) {
- int c = a + B;
- return C;
- };
- NSLog (@"before Blocks2");
- int ret = MYBLOCKS2 (ten, 20);
- NSLog (@"after BLOCKS2 ret%d", ret);
- //Here if you do not add __block will be an error
- __blockint sum = 0;
- Int (^MYBLOCKS3) (int A, int b) = ^ (int A, int b) {
- sum = a + b;
- RETURN3;
- };
- MYBLOCKS3 ( 30);
- NSLog (@"sum is%d", sum);
- }
- Returnnsapplicationmain (argc, (Constchar * *) argv);
- }
#import <cocoa/cocoa.h>int Main (int argc, char *argv[]) {@autoreleasepool { NSLog (@ "Hello world"); void (^myblocks) (void) = NULL; Myblocks = ^ (void) {NSLog (@ "in blocks"); }; NSLog (@ "before Myblocks"); Myblocks (); NSLog (@ "after Myblocks"); Int (^MYBLOCKS2) (int a, int b) = ^ (int a, int b) {int c = a + B; return C; }; NSLog (@ "before Blocks2"); int ret = MYBLOCKS2 (10, 20); NSLog (@ "After BLOCKS2 ret%d", ret); If you do not add __block here will be an error __blockint sum = 0; Int (^MYBLOCKS3) (int a, int b) = ^ (int a, int b) {sum = a + b; RETURN3; }; MYBLOCKS3 (20, 30); NSLog (@ "sum is%d", sum); } returnnsapplicationmain (argc, (Constchar * *) argv);}
Printing results are as follows 2012-09-03 10:23:20.878 blocktest[407:403] Hello world 2012-09-03 10:23:20.880 blocktest[407:403] before myblocks 2012-09-03 10:23:20.881 blocktest[407:403] in blocks 2012-09-03 10:23:20.881 blocktest[407:403] after myblocks 2012-09-03 10:23:20.882 blocktest[407:403] before Blocks2 2012-09-03 10:23:20.882 blocktest[407:403] after BLOCKS2 ret 30 2012-09-03 10:23:20.882 blocktest[407:403] sum is 50
Vii. Block Write callback example 1, Dog.h
Java code
- #Import <Foundation/Foundation.h>
- @interface Dog:nsobject {
- int _id;
- Nstimer *timer;
- int barkcount;
- //define a blocks variable
- void (^barkcallback) (Dog *thisdog, int count);
- }
- @property (assign) int ID;
- Exposing an interface outward
- -(void) Setbark: ( void (^) (Dog *thisdog, int count)) Eachbark;
- @end
#import <Foundation/Foundation.h> @interface dog:nsobject { int _id; Nstimer *timer; int barkcount; Defines a blocks variable void (^barkcallback) (Dog *thisdog, int count);} @property (assign) int id;//exposes an interface-(void) Setbark: (void (^) (Dog *thisdog, int count)) Eachbark; @end
2, DOG.M
Java code
- #Import "Dog.h"
- @implementation Dog
- @synthesize ID = _id;
- -(ID) init
- {
- Self = [Superinit];
- if (self) {
- //every 1s call updatetimer method
- Timer = [Nstimerscheduledtimerwithtimeinterval:1.0ftarget:selfselector:@selector (updatetimer:) UserInfo: Nilrepeats:yes];
- }
- returnself;
- }
- -(void) Updatetimer: (ID) arg
- {
- Barkcount + +;
- NSLog (@"dog%d bark count%d", _id, Barkcount);
- //reporting to person objects
- if (barkcallback) {
- //Call blocks sent from person
- Barkcallback (self, barkcount);
- }
- }
- -(void) Setbark: (void (^) (Dog *, int)) Eachbark
- {
- [Barkcallbackrelease];
- Barkcallback = [Eachbark copy];
- }
- -(void) Dealloc
- {
- [Barkcallbackrelease];
- [Superdealloc];
- }
- @end
#import "Dog.h" @implementation dog@synthesize id = _id;-(ID) init{Self = [Superinit]; if (self) { //every 1s call once updatetimer method timer = [Nstimerscheduledtimerwithtimeinterval:1.0ftarget:selfselector : @selector (Updatetimer:) UserInfo:nilrepeats:YES]; } Returnself;} -(void) Updatetimer: (ID) arg{ barkcount + +; NSLog (@ "Dog%d bark count%d", _id, Barkcount); Report to the Person object if (barkcallback) { //Call blocks barkcallback (self, barkcount) passed from person;} } -(void) Setbark: (void (^) (DOG *, int)) eachbark{ [barkcallbackrelease]; Barkcallback = [Eachbark copy];} -(void) dealloc{ [barkcallbackrelease]; [Superdealloc];} @end
3, Person.h
Java code
- #Import <Foundation/Foundation.h>
- #Import "Dog.h"
- @interface Person:nsobject
- {
- Dog *_dog;
- }
- @property (retain) Dog *dog;
- @end
#import <Foundation/Foundation.h> #import "Dog.h" @interface person:nsobject{ Dog *_dog;} @property (retain) Dog *dog; @end
4, PERSON.M
Java code
- #Import "Person.h"
- @implementation person
- @synthesize dog = _dog;
- -(void) Setdog: (dog *) Dog
- {
- if (_dog! = dog) {
- [_dogrelease];
- _dog = [dog retain];
- [_dogsetbark:^ (Dog *thisdog, int count) {
- NSLog (@"person dog%d count%d", [Thisdog ID], count);
- }];
- }
- }
- -(dog *) dog
- {
- Return_dog;
- }
- -(void) Dealloc
- {
- Self.dog = nil;
- [Superdealloc];
- }
- @end
#import "Person.h" @implementation person@synthesize dog = _dog;-(void) Setdog: (dog *) dog{ if (_dog! = dog) { [_do Grelease]; _dog = [dog retain]; [_dogsetbark:^ (Dog *thisdog, int count) { NSLog (@ "Person dog%d count%d", [Thisdog ID], count); }];} } -(DOG *) dog{ Return_dog;} -(void) dealloc{ self.dog = nil; [Superdealloc];} @end
5, MAIN.M
Java code
- #Import <Foundation/Foundation.h>
- #Import "Person.h"
- #Import "Dog.h"
- int main (int argc, Constchar * argv[])
- {
- @autoreleasepool {
- //Insert code here ...
- NSLog (@"Hello, world!");
- Person *person = [[Personalloc] init];
- Dog *dog = [[Dogalloc] init];
- [Dog SetID:10];
- [Person Setdog:dog];
- [Dog release];
- While (1) {
- [[Nsrunloopcurrentrunloop] run];
- }
- [Person release];
- }
- return 0;
- }
#import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main (int ARGC, Constchar * argv[]) {@autoreleasepool {//Insert code here ... NSLog (@ "Hello, world!"); Person *person = [[Personalloc] init]; Dog *dog = [[Dogalloc] init]; [Dog Setid:10]; [Person Setdog:dog]; [Dog release]; while (1) {[[Nsrunloopcurrentrunloop] run]; } [person release]; } return 0;}
6. Printing results 2012-09-03 11:21:08.551 blockdelegate[549:403] Hello, world! 2012-09-03 11:21:09.554 blockdelegate[549:403] Dog bark count 1 2012-09-03 11:21:09.555 blockdelegate[549:403] Person D OG count 1 2012-09-03 11:21:10.554 blockdelegate[549:403] Dog bark count 2 2012-09-03 11:21:10.555 blockdelegate[549 : 403] Person dog count 2 2012-09-03 11:21:11.553 blockdelegate[549:403] Dog bark count 3 2012-09-03 11:21:11.554 Blo CKDELEGATE[549:403] Person Dog ten count 3 2012-09-03 11:21:12.554 blockdelegate[549:403] Dog bark count 4 2012-09-03 11 : 21:12.555 blockdelegate[549:403] Person dog count 4 2012-09-03 11:21:13.553 blockdelegate[549:403] Dog bark Count 5 2012-09-03 11:21:13.553 blockdelegate[549:403] Person dog count 5 2012-09-03 11:21:14.553 blockdelegate[549:403] Dog 1 0 Bark Count 6 2012-09-03 11:21:14.554 blockdelegate[549:403] Person Dog Count 6
The application of the block of iOS learning notes in iOS development