Objective-C Special Syntax: @ property, @ synthesize

Source: Internet
Author: User

08-Objective-C special Syntax: @ property, @ synthesize 1. @ property is used in @ interface to automatically generate the setter (setter) and getter (constructor) declarations using @ property int age; you can replace the following code:-(void) setAge: (int) age; // setter-(int) age; // getter 2. @ synthesize is used in @ implementation to automatically generate setter (setter) and getter (constructor) implementations. @ synthesize age = _ age can replace the following code: -(void) setAge :( int) age {_ age = age;}-(int) age {return _ age;} if the member Variable _ age does not exist, the @ private Variable _ age is automatically generated (because _ age is declared in @ implementation, it is private) if it is @ synthesize age; in this case, specify the age to be implemented, if. if another age member variable with the same name is declared in the H file, its setter and getter will be implemented. Otherwise, an age variable will be automatically generated in @ implementation, if the setter and getter methods of age are manually implemented, the compiler automatically implements the getter method. If the getter method is manually implemented, compilation automatically implements the setter method. If both setter and getter are implemented at the same time, the compiler will not automatically generate nonexistent member variables. after Xcode4.4: feature @ property includes the @ synthesize function, that is, declare @ propery int age in @ interface; the compiler is in. the setter and getter methods are automatically generated in the hfile. By default, the member variables of underline _ age are accessed, @ proterty automatically generated member variables are protected by @ protect by default (because in. member variables declared in the H file. The default value is @ protect)

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.