Objective-c: Custom Block function

Source: Internet
Author: User



The block function is a function pointer-like function, the programmer only need to put the code needs to be encapsulated in the definition of the block can be used in the future, the direct invocation, very convenient ....






Examples are as follows:



Customizing a block with no parameters


#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
     @autoreleasepool
     {
    
         //The first form: custom block function type without parameters
         typedef void (^FirstBlock)(void);
        
         //Create a block and encapsulate the code
         FirstBlock block = ^(void){
             for (int i=0; i<5; i++)
             {
                 NSLog(@"i:%d",i);
             }
         };
        
         //Call the block function
         block();
   
     return 0;
} 


Operation Result:


2015-10-17 18:38:35.317 Custom Block function [2507:145127] i:0
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:1
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:2
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:3
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:4
Program ended with exit code: 0 





Customizing a block with parameters


#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
     @autoreleasepool
     {
         //The second form: custom block function type with parameters
         typedef void (^SecondBlock)(int);
        
         //Create a block and encapsulate the code
         SecondBlock block = ^(int length){
             for (int i=0; i<length; i++)
             {
                 NSLog(@"i:%d",i);
             }
         };
        
         //Call the block function
         block(5);
     }
    
     return 0;
} 


Operation Result:


2015-10-17 18:38:35.317 Custom Block function [2507:145127] i:0
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:1
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:2
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:3
2015-10-17 18:38:35.319 Custom Block function [2507:145127] i:4
Program ended with exit code: 0 





Objective-c: Custom Block function


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.