objective-c - How to assign values to member variables
怪我咯2017-05-02 09:27:14
0
2
576
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?
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.
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:
A getter method will also be generated, which looks like this:
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.