Home  >  Q&A  >  body text

objective-c - 成员变量如何赋值的

我们都知道属性的赋值是调用属性的setter方法, 可是成员变量是如何赋值的呢? 他的内部实现原理是什么?

怪我咯怪我咯2698 days ago493

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-05-02 09:29:14

    @implementation ViewController
    
    {
        UIView *_iconView;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSLog(@"%@", self->_iconView);
        NSLog(@"%@", _iconView);
    }

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:29:14

    I think I want to ask about the difference between ivar and property.
    property = ivar+getter+setter

    For example
    property (assign) int a will generate a member variable of _a by default, which we call ivar
    At the same time, a setter method will be generated, which looks like this:

    -(void)setA:(int)a {
        _a = a;
    }
    

    A getter method will also be generated, which looks like this:

    -(int)a {
        return _a;
    }
    

    At the same time, it will give _a some gain buffs, such as strong strong reference and weak weak reference to control the life cycle of this variable.

    So property is just a form of programming, you don’t need to pay too much attention to it, you can completely follow your own routine.

    reply
    0
  • Cancelreply