Blockの基本使用、typedefがBlockに別名を定義する、Blockの適用シーン、Blockの注意事項


Blockとは?
Blockはiosの中で1種の比較的に特殊なデータ型で、アップルの公式の特に推薦して使う1種のデータ型で、応用シーンはあります:アニメーション、マルチスレッド、集合遍歴、ネットの要求のコールバックなど
Blockの役割:あるコードを保存するために使用され、適切な時間に取り出して呼び出すことができます.
Blockの基本的な使用:
//
//  main.m
//  Block     
//
//  Created by XinYou on 15-5-8.
//  Copyright (c) 2015  vxinyou. All rights reserved.
//

#import 

/**
 *           、     block
 */
void test_1(){
    
    //   block
    void (^helloWorld)() = ^{
        
        NSLog(@"Hello World");
    };
    
    //   block
    helloWorld();
}

/**
                 block
 */
void test_2()
{
    //   block, block         Hello World
    void (^helloWorld)(int) = ^(int count){
    
        for (int i = 0; i < count; i++) {
            
            NSLog(@"Hello World");
        }
    };
    
    //   block
    helloWorld(5);
}

/**
          、    block
 */
void test_3(){

    //   block, block    
    int (^sumBlock) (int, int) = ^(int a, int b){
    
        return a + b;
    };
    
    //   block
    int sum = sumBlock(8, 9);
    
    /* 
     
     block      :
     
    int (^myBlock) (int, int);
    
    myBlock = ^(int a, int b){
    
        return a -b;
    };
    */
    
    NSLog(@"%d", sum);
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
//        test_1();
        
//        test_2();
        
        test_3();
    }
    return 0;
}


Blockとtypedef(Blockに別名を定義):
//
//  main.m
//  Block typedef
//
//  Created by XinYou on 15-5-8.
//  Copyright (c) 2015  vxinyou. All rights reserved.
//

#import 

//   typedef          
typedef int MyInt;

//   typedef block   
typedef int (^MyBlock)(int, int);

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        /*
        int (^minusBlock) (int, int) = ^(int a, int b){
        
            return a - b;
        };
        
        int (^multiBlock) (int, int) = ^(int a, int b){
        
            return a * b;
        };
        
        */
        
        //               ,          
        
        MyBlock minusBlock = ^(int a, int b){
            
            return a - b;
        };
        
        MyBlock multiBlock = ^(int a, int b){
            
            return a * b;
        };
        
        
        NSLog(@"%d", minusBlock(10, 7));//     :3
        
        NSLog(@"%d", multiBlock(8, 7));//     :56
    }
    return 0;
}


Blockの適用シーン:
//
//  main.m
//  Block     
//
//  Created by XinYou on 15-5-8.
//  Copyright (c) 2015  vxinyou. All rights reserved.
//

#import 

/**
 *         ,   3      
 */


/***************************************      begin*********************************/
 
//            
void doWorkOfFirstDay(){

    NSLog(@"  、  、     ");
    NSLog(@"  、   ");
    NSLog(@"    ");
    
    /********************/
    NSLog(@"       ");
    /********************/
    
    NSLog(@"        ");
    NSLog(@"   ");
    NSLog(@"  ");
    NSLog(@"  ");
}

//            
void doWorkOfSecondDay(){
    
    NSLog(@"  、  、     ");
    NSLog(@"  、   ");
    NSLog(@"    ");
    
    /********************/
    NSLog(@"         ");
    /********************/
    
    NSLog(@"        ");
    NSLog(@"   ");
    NSLog(@"  ");
    NSLog(@"  ");
}

//            
void doWorkOfThirdDay(){
    
    NSLog(@"  、  、     ");
    NSLog(@"  、   ");
    NSLog(@"    ");
    
    /********************/
    NSLog(@"      ");
    /********************/
    
    NSLog(@"        ");
    NSLog(@"   ");
    NSLog(@"  ");
    NSLog(@"  ");
}

/***************************************      end*********************************/


/***************************************      begin*********************************/

void workBefore(){

    NSLog(@"  、  、     ");
    NSLog(@"  、   ");
    NSLog(@"    ");
}

void workAfter(){

    NSLog(@"        ");
    NSLog(@"   ");
    NSLog(@"  ");
    NSLog(@"  ");
}

//            
void doWorkOfFirstDay_1(){
    
    workBefore();
    
    /********************/
    NSLog(@"       ");
    /********************/
    
    workAfter();
}

//            
void doWorkOfSecondDay_1(){
    
    workBefore();
    
    /********************/
    NSLog(@"         ");
    /********************/
    
    workAfter();
}

//            
void doWorkOfThirdDay_1(){
    
    workBefore();
    
    /********************/
    NSLog(@"      ");
    /********************/
    
    workAfter();
}

/***************************************      end*********************************/


/***************************************Block   begin*********************************/

void doWorkOfOneDay(void (^work)()){
    
    NSLog(@"  、  、     ");
    NSLog(@"  、   ");
    NSLog(@"    ");
    
    /********************/
    if (work != nil) {//      ,   block nil,     
        work();
    }
    /********************/
    
    NSLog(@"        ");
    NSLog(@"   ");
    NSLog(@"  ");
    NSLog(@"  ");
}

//            
void doWorkOfFirstDay_2(){
    
    doWorkOfOneDay(^{
        NSLog(@"       ");
    });
}

//            
void doWorkOfSecondDay_2(){
    
    doWorkOfOneDay(^{
        NSLog(@"         ");
    });
    
}

//            
void doWorkOfThirdDay_3(){
    
    doWorkOfOneDay(^{
        NSLog(@"      ");
    });
}

/***************************************Block   end*********************************/

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        doWorkOfFirstDay_2();
        doWorkOfSecondDay_2();
        doWorkOfThirdDay_3();
        
    }
    return 0;
}


Blockの注意事項:
//
//  main.m
//  Block    
//
//  Created by XinYou on 15-5-8.
//  Copyright (c) 2015  vxinyou. All rights reserved.
//

#import 

typedef void (^MyBlock) ();

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        int a = 10;
        
        __block int b = 10;
        
        MyBlock block = ^{
        
            //        ,     ,block             
//            a = 20;
            
            //         , __block       ,   block    
            b = 20;
            
            NSLog(@"a = %d", a);//       ,      
            
            NSLog(@"b = %d", b);
        };
        
        block();
        
    }
    return 0;
}