IOS assign和weak的区别

  • 面试:解释weak,assgin,什么时候使用Weak和assign
    ARC:才有weak
    weak:__weak 弱指针,不会让引用计数器+1,如果指向对象被销毁,指针会自动清空
    assgin:__unsafe_unretained修饰,不会让引用计数器+1,如果指向对象被销毁,指针不会清空 ,若果再引用这个对象就会崩毁 Thread 1: EXC_BAD_ACCESS (僵尸对象)
@interface ViewController ()
@property (nonatomic, assign)  UIView *testView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 2, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    self.testView = redView;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.testView .backgroundColor = [UIColor orangeColor];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容