block


- (void)testBlockVariable {

    //10,注意outsiderVariable这里是10.,使用的是定义时候的只读拷贝。

    NSInteger outsiderVariable = 10;

    NSMutableArray *outsideArray = [[NSMutableArray alloc] init];

    

    void (^blockObject)(void) = ^(void){

        NSInteger insideVariable = 20;

        //30

        NSLog(@">member variable = %d", self.memberVariable);

        //10

        NSLog(@">outside variable = %d",outsiderVariable);

        //20

        NSLog(@">inside variable = %d",insideVariable);

        //这里居然可以变

        [outsideArray addObject: @"AddedInsideBlock"];

    };

    outsiderVariable = 30;

    self.memberVariable = 30;

    blockObject();

    NSLog(@">%d items in outsideArray",[outsideArray count]);

}


-(void)testBlock{

    //2-3-3,两个小翅膀,三个小括号,三个void

    //1,定义

    void (^aBlock) (void) = ^(void){

        NSLog(@"hello");

    };

    aBlock();

    

    //2,变形

    void (^bBlock) (void) = 0;

    bBlock = ^(void){

        NSLog(@"别来无恙啊");

    };

    bBlock();

    

    //3,数组

    void (^CBlock[2]) (void) = {

        ^(void){

            NSLog(@"1");

        },

        ^(void){

            NSLog(@"2");

        }

    };

    CBlock[0]();

    CBlock[1]();

    

    //4,外部变量,不能改,要改的话加static或者__block

   int global = 100;

    __block int blockLocal = 100;

    static int staticLocal = 100;

    void (^dBlock)(void) = ^(void){

        NSLog(@">>Sum:%d\n",global + staticLocal);

       // global ++;

        blockLocal ++;

        staticLocal ++;

    };

    dBlock();

    NSLog(@"After modified,global:%d,block local:%d,static local:%d\n",global,blockLocal,staticLocal);

    

    //5, static+const来构成block递归

    void (^eBlock)(int) = 0;

    static void (^const staticBlock)(int) = ^(int i){

        if (i>0) {

            NSLog(@">>static %d",i);

            staticBlock(i -1 );

        }

    };

    eBlock = staticBlock;

    eBlock(5);

    

    //6,__block来构成block递归

    __block void(^blockBlock)(int) = 0;

    blockBlock = ^(int i){

        if (i>0) {

            NSLog(@">>block %d",i);

            blockBlock(i-1);

        }

    };

    blockBlock(5);

    

    //dispatch_semaphore_t来实现queue之间通讯。

    //task2必须等待task1执行完毕,主线程必须等待task2执行完毕。

    int length =10;

    int data[length];

    for (int i=0; i<length; i++) {

        data[i] = i;

    }

    __block int *pArray = data;

    //创建semaphore

    __block dispatch_semaphore_t sem1 = dispatch_semaphore_create(0);

    __block dispatch_semaphore_t sem2 = dispatch_semaphore_create(0);


    dispatch_queue_t queue = dispatch_queue_create("StudyBlocks", NULL);

    __block int s=0;

    dispatch_block_t task1 = ^(void){

        int sum = 0;

        for (int i=0; i<length ; i++) {

            sum += pArray[i];

            //sleep(1000);

            

        }

        s = sum;

        NSLog(@">>Sum:%d",sum);

        //发送一个semaphore

        dispatch_semaphore_signal(sem1);

    };

    dispatch_block_t task2 = ^(void){

        dispatch_semaphore_wait(sem1, DISPATCH_TIME_FOREVER);

        int sum = s;

        for (int i = 0; i<length; i++) {

            sum-= pArray[i];

        }

        s = sum;

        NSLog(@">>after subtract:%d",sum);

        dispatch_semaphore_signal(sem2);

    };

    

    dispatch_async(queue, task1);

    dispatch_async(queue, task2);

    

    //等待一个semophore

    dispatch_semaphore_wait(sem2, DISPATCH_TIME_FOREVER);

    NSLog(@"-------------");

    //灭掉这个semaphore

    //dispatch_release(sem);

    //dispatch_release(queue);

    

    //7dispatch_apply实现并行求和

    dispatch_queue_t queueApply = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    __block int sum = 0;

    dispatch_apply(length,queueApply, ^(size_t i) {

        sum+=pArray[i];

    });

    NSLog(@">>sum:%d",sum);

    //dispatch_release(queueApply);

    

    //8dispatch_group_t来插一脚

    __block int sum8 = 0;

    __block dispatch_semaphore_t taskSem8 = dispatch_semaphore_create(0);

    dispatch_queue_t queue8 = dispatch_queue_create("StudyBlocks8", NULL);

    dispatch_group_t group8 = dispatch_group_create();

    dispatch_block_t task18 = ^(void){

        int s = 0;

        for (int i= 0; i<length; i++) {

            s+= pArray[i];

        }

        sum8 = s;

        NSLog(@">>after add:%d",sum);

        dispatch_semaphore_signal(taskSem8);

    };

    

    dispatch_block_t task28 = ^(void){

        dispatch_semaphore_wait(taskSem8, DISPATCH_TIME_FOREVER);

        int s = sum8;

        for (int i=0; i<length; i++) {

            s-= pArray[i];

        }

        sum8 = s;

        NSLog(@">>after substract : %d",sum8);

        

    };

    dispatch_group_async(group8, queue8, task18);

    dispatch_group_async(group8, queue8, task28);

    dispatch_group_wait(group8, DISPATCH_TIME_FOREVER);

    

    

}

你可能感兴趣的:(block)