Introduction and usage of block in objective-c language.

Source: Internet
Author: User

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.

Related Article

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.