The difference between the @property and Ivar of IOS Basics _ios

Source: Internet
Author: User

@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.

p.name1 = @ "abc";

This line of code is actually a setter method that invokes the name1 in person.

NSLog (@ "%@", p.name1);

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.