Attribute (@ property), @ synthesize, propertysynthesize

Source: Internet
Author: User

Attribute (@ property), @ synthesize, propertysynthesize

The previous instance variables we learned are as follows:

1 {2     int _age;3     int _height;4     int age;5 }    

Learn attributes later

1 @property int age;

@ Property will automatically compile and generate the setter method and getter method declaration of a member variable.

1 - (void)setAge:(int) age;2 - (int)age;

Example:

@ Property int _ age;

Compile and generate

-(Void) set_age :( int) age;

-(Int) _ age;

That is to say, how do you compile and generate instance variables? In order to generate the declaration of the setter method and getter method of the standard instance variables, do not add _ to the attribute and set the setAge to uppercase.

 

Since @ property has the setter method of the instance variable and the declaration of the getter method, the implementation is @ synthesize property.

1 // write the attribute later. Specify the attribute to implement the setter and getter Methods 2 @ synthesize age.

We can see that @ synthesize will automatically compile and generate the setter and getter methods of a member variable.

1 - (void)setAge:(int)age {2   _age = age;  3 }4 5 - (int)age {6   return age;  7 }

If you want to access the member Variable _ age

@ Synthesize age = _ age;

In this way, the setter and getter methods of age are automatically generated, and the member Variable _ age is accessed.

Note: age on the left: the setter and getter methods of age must be implemented.

Age on the right: access the member variable "_ age" in the implementation. In this way, the attribute is associated with the instance variable.

Required member variables to be modified

 

The above @ synthesize age; if no member variable is specified, the system accesses the age by default.

In this case, the member variable "_ age" is automatically accessed. If the member variable "_ age" does not exist, the variable "@ private" is automatically generated. The variable "_ age" is private in the extension.

 

After XCode4.n @ property, the Declaration is generated and implemented.

1 @property int age;

Attribute is the description and implementation of a pair of setter and getter methods of the Instance Variable _ age.

By default, the setter and getter Methods access the instance variables starting _.

 

After learning this, we know why the member variables start with _ at the beginning. In fact, this is the case inside the system.

 

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.