Ios learning notes block in ios development applications, ios learning notes
1. What is Blocks?Block is a C-level syntax and a feature of runtime. It is similar to a function (function pointer) in Standard C, but its operation requires support from the compiler and runtime, block has been well supported since ios4.0.
Ii. When to use Block during ios developmentIn addition to defining the parameter list and return type, Block can also obtain the state within the defined lexical range (such as local variables ), the status can be modified under certain conditions (such as using the _ block Variable. In addition, these modifiable states are shared among multiple blocks in the same lexical range, even if the lexical range (such as stack expansion and scope) exists ), you can continue to share or modify these statuses. Generally, blocks are the encapsulation of some short code snippets and are applicable to work units. They are usually used for concurrent tasks, traversal, and callback.
3. How to declare block (compared with function declaration in C) 4. c function correction and blocks callInt (* CFunc) (int a) function call int result = CFunc (10); int (^ BFunc) (int a) function call int result = BFunc (10 );
5. _ block keywordsA Block can reference variables out of its own scope, including static variables, extern variables, or free variables (if a variable is defined without a storage modifier, by default, the free variables auto and auto are saved in the stack. In addition to auto, there are also storage modifiers such as register and static. For free variables, read-only in the Block. When block is introduced, a special _ block keyword variable storage modifier is introduced.
6. 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 (10, 20 );
NSLog (@ "after blocks2 ret % d", ret );
// If _ block is not added, an error is returned.
_ 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 );
}
# 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 NO _ block is added, the following error occurs: _ 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 );}
The printed result is as follows: 10:23:20. 878 blockTest [407: 403] Hello world 10:23:20. 880 blockTest [407: 403] before myblocks 10:23:20. 881 blockTest [407: 403] in blocks 10:23:20. 881 blockTest [407: 403] after myblocks 10:23:20. 882 blockTest [407: 403] before blocks2 10:23:20. 882 blockTest [407: 403] after blocks2 ret 30 10:23:20. 882 blockTest [407: 403] sum is 50
VII. Example of block write callback1. Dog. h
Java code
# Import <Foundation/Foundation. h>
@ Interface Dog: NSObject {
Int _ ID;
Nstmer * timer;
Int barkCount;
// Define a blocks variable
Void (^ BarkCallback) (Dog * thisDog, int count );
}
@ Property (assign) int ID;
// Expose an interface externally
-(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 externally-(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 ){
// Call the updateTimer method once every 1 s
Timer = [NSTimerscheduledTimerWithTimeInterval: 1.0 ftarget: 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 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) {// call the updateTimer method once every 1 s timer = [timer: 1.0 ftarget: 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 the Blocks BarkCallback (self, barkCount) ;}}-(void) setBark :( void (^) (Dog *, int) passed from Person )) 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) { [_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
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 ){
[[Nsunloopcurrentrunloop] 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. Print the result 11:21:08. 551 blockDelegate [549: 403] Hello, World! 11:21:09. 554 blockDelegate [549: 403] dog 10 bark count 1 11:21:09. 555 blockDelegate [549: 403] person dog 10 count 1 11:21:10. 554 blockDelegate [549: 403] dog 10 bark count 2 11:21:10. 555 blockDelegate [549: 403] person dog 10 count 2 11:21:11. 553 blockDelegate [549: 403] dog 10 bark count 3 11:21:11. 554 blockDelegate [549: 403] person dog 10 count 3 11:21:12. 554 blockDelegate [549: 403] dog 10 bark count 4 11:21:12. 555 blockDelegate [549: 403] person dog 10 count 4 11:21:13. 553 blockDelegate [549: 403] dog 10 bark count 5 11:21:13. 553 blockDelegate [549: 403] person dog 10 count 5 11:21:14. 553 blockDelegate [549: 403] dog 10 bark count 6 11:21:14. 554 blockDelegate [549: 403] person dog 10 count 6