iOS- 基础性问题

1、 weak和assign的区别?

  • iOS开发中 weak和assign的区别
  • assin与weak的区别
/* __unsafe_unretained(不安全的,不引用-不持有)
 * 如果指针指向的对象被销毁,但是 assign 并没有把指针清空
 * 不会让引用计数器+1
 */
@property (nonatomic, assign) UIView *magentaView;

/* weak 一般用于修饰控件
 * 如果指针指向的对象被销毁,那么 weak 会让这个指针也清空
 * ARC才有,不会让引用计数器+1
 */
@property (nonatomic, weak) UIView *redView;


//-----------------------------------------------------------
- (void)configUI {
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:view1];
    self.magentaView = view1;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    self.redView = view;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _magentaView.frame = CGRectMake(50, 50, 100, 100);
    _redView.frame = CGRectMake(200, 50, 100, 100);
}

你可能感兴趣的:(iOS- 基础性问题)