Block 作為塊語(yǔ)法 使用 __weak __strong
想必 在block 外使用weak 大家都明白,
最近使用的時(shí)候在內(nèi)部strong 發(fā)現(xiàn) block 持有對(duì)象 這樣又出現(xiàn)相互持有的鬼東西
最后發(fā)現(xiàn)自己錯(cuò)
原來是作用域自己沒理解
#define LRWeak(type) __weak typeof(type) weak##type = type;
#define LRStrong(type) __strong typeof(type) type = weak##type;
LRShop * shop = [[LRShop alloc]init];
shop.string = @"xxxxxx4444";
LRWeak(shop)
shop.myBlock = ^{
LRStrong(shop)
// NSLog(@"%@",shop.string);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",shop.string);
});
};
block 內(nèi)部的strong 是一個(gè)局部變量(shop PS 只是和外部對(duì)象名字一樣罷了)
當(dāng)block 執(zhí)行完了就會(huì)釋放持有對(duì)象
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者