Effective Objective-C 2.0

📚Effective OC - Tip 37,38: 理解 bl

2017-01-10  本文已影响10人  小万叔叔

^ block 的代言人

void (^ name) (int a)
//Swift de closure 就更直接了
() -> T 
- (void)testingHeapBlock:(BOOL)condition {
    //标准的栈block,在ARC模式下已经变成了堆block,MRC才是栈Block,都为NSMallocBlock
    void (^stackBlock)() = ^ {
        NSLog(@"enter stackBlock");
        self.current = @"123";
    };
    stackBlock();
    
    void (^heapBlock)();//放到函数外部就会造成循环引用,内部不会
    //这样写就变成了堆block了,因为有了赋值操作
    heapBlock = ^{
        NSLog(@"enter heapBlock");
        self.current = @"123";
    };
    heapBlock();
}

//NSGlocalBlock, 不捕获状态,所以也就不会循环引用
void (^gobalBlock)(BlockViewController *type) = ^(BlockViewController *type){
    NSLog(@"global block is %@", type.current);
};

    typedef void (^TypeBlock)(int a, int b);
    TypeBlock typeBlock = ^(int a, int b) {
        NSLog(@"enter typeBlock");
    };

Extension

block 与 delegate 的区别

上一篇下一篇

猜你喜欢

热点阅读