美文网首页
iOS- 基础性问题

iOS- 基础性问题

作者: lukyy | 来源:发表于2021-11-21 23:27 被阅读0次

    1、 weak和assign的区别?

    /* __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<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        _magentaView.frame = CGRectMake(50, 50, 100, 100);
        _redView.frame = CGRectMake(200, 50, 100, 100);
    }
    

    相关文章

      网友评论

          本文标题:iOS- 基础性问题

          本文链接:https://www.haomeiwen.com/subject/uuzmtrtx.html