@property
A property is actually an encapsulation of a member variable. Let's think about this first:
@property = Ivar + setter + getter
Ivar
Ivar can be understood as a variable in a class, and the primary role is to save data.
Let's take a look at an example, and we can explain these two things clearly by using the example below:
We create a new person class
@interface person:nsobject
{
nsstring *NAME0;
}
@property (nonatomic,copy) nsstring *name1;
@end
@implementation Person
-(instancetype) init {
if (self = [super init]) {
} return
self;< c15/>}
@end
In this person NAME0 is the member variable, name1 is the attribute.
We create a person:
Person *p= [[Person alloc] init];
p.name1 = @ "abc";
NSLog (@ "%@", p.name1);
What does it mean that I can't access NAME0 outside of the person class? This indicates that the member variable <font color=red>name0</font> can only be accessed inside its own class.
Therefore, we infer that @property actually has interface properties, that is, can be accessed by external objects.
This line of code is actually a setter method that invokes the name1 in person.
This line of code is actually the Getter method that invokes the name1 in person.
Again, the setter and getter methods. We should all know that OC has a strict naming code, take this example, according to NAME1 automatically generated
-(void) setName1: (NSString *) name1{}
-(NSString *) name1
Note: The MRC is not discussed here, and all explanations are provided under arc.
@synthesize
This keyword is used to specify the member variable
In the implementation of person, we change the code to this way:
@implementation person
@synthesize name1 = _name2;
-(Instancetype) init {
if (self = [super init]) {
_name2 = @ "AAA";
}
return self;
}
@end
This allows us to specify that the NAME1 member variable is _name2, and we cannot _name1 this attribute at all in the initialization init method of person.
Person *p= [[Person alloc] init];
p.name1 = @ "abc";
NSLog (@ "%@", p.name1);
We comment out the assigned line and we can see that the printout is: AAA.
The above is a small set to introduce the iOS basic knowledge of the @property and Ivar differences, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!