The block syntax was added in the iOS4.0 SDK , where more than a dozen APIs were used to block syntax.
The block object is also inherited and NSObject, and is a C-language-level syntax and runtime feature . Similar to the standard C function pointer . However, it is possible to maintain state data for similar objects in addition to executable statements .
Similar to the C language function pointers, but not exactly the same. Commonly used as a callback function.
Block vs. function operators:
The function pointer uses the ' * '
Block uses ' ^ '
Here is an example:
Apple.h file
#import <Foundation/Foundation.h>@interfacevoid (^SHUIGUO1) (void Void (^SHUIGUO2) (void Void (^SHUIGUO3) (void); -(void) Shuiguo; @end
APPLE.M file
#import "apple.h"@implementationApple- (void) shuiguo{if(_SHUIGUO1)//Determine if the _shuiguo1 is 0 {NSLog (@"I'm an apple ."); } if(_SHUIGUO2) {NSLog (@"I'm a banana ."); } if(_SHUIGUO3) {NSLog (@"I'm an orange ."); }}@end
Main function
#import<Foundation/Foundation.h>#import "apple.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Apple*app =[[Apple Alloc]init]; Create an App object App.shuiguo1= ^{//print a shuiguo1 bolck. NSLog (@"I'm an apple ."); }; App.shuiguo2= ^{NSLog (@"I'm a banana ."); }; App.shuiguo3= ^{NSLog (@"I'm an orange ."); }; [App Shuiguo]; method to invoke Shuiguo}return 0;}
Introduction and usage of block in objective-c language.