search

Home  >  Q&A  >  body text

objective-c - How to assign values ​​to member variables

We all know that the assignment of attributes is to call the setter method of the attribute, but how are member variables assigned? What is its internal implementation principle?

怪我咯怪我咯2809 days ago580

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