BLock 类型
Block可以捕获外界变量 copy
001 栈 Block
int a = 10;
NSLog(@"%@",^{
NSLog(@"hello world ----%d",a);
})
NSLog(@"%@",block);
002 堆 Block
int a = 10;
void (^block)(void) = ^{
NSLog(@"hello world ----%d",a);
}
NSLog(@"%@",block);
003 全局 Block
void (^block)(void) = ^{
NSLog(@"hello world");
}
NSLog(@"%@",block);
=是操作符
循环引用:A和B相互持有 都等着对方发送retaincount=0的指令
self.block = ^{
self.name = @"hello";
}
这会导致循环引用
解决版本1
__weak typeof(self) weakSelf = self;
self.block = ^{
weakSelf.name = @"hello";
}
解决版本2
__strong typeof(self) strongSelf = self;
self.block = ^{
strongSelf.name = @"hello";
}
个人理解:首先weakSelf和strongSelf 都是临时变量都可以在autorealsepool中进行释放,区别在于strongSelf 引用计数+1 可以持有self的变量进行操作, weakSelf引用计数减一对象被释放所以就无法使用self的变流量
来源:CSDN
作者:Coding_Physical
链接:https://blog.csdn.net/Coding_Physical/article/details/104044952