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 *)touches withEvent:(UIEvent *)event
{
    self.testView .backgroundColor = [UIColor orangeColor];
}

你可能感兴趣的:(IOS assign和weak的区别)