iOS development OC (vii)--@property and @synthesize

Source: Internet
Author: User

@property and @synthesize

First, from the perspective of translation: @property (translated to: Nature) and @synthesize (synthesis), is not much like attributes (declarations) and implementation?

1. @property

Used in @inteface.

declaration used to automatically generate setter and getter

with @property int age, you can replace the following two lines

-(void) Setage: (int) age;  // Setter-(int) age;   // Getter

[Email protected] (Learn only)

Used in @implementation.

implementation to automatically generate setter and getter

With @synthesize age = _age, you can replace

-(int) age{    return  _age;} -(void) Setage: (int) age{    = Age ;}

3. @synthesize Details (learn only)

    • @synthesize age = _age;

The member variables are accessed in the > Setter and Getter implementations _age

> If the member variable _age does not exist, a @private member variable is automatically generated _age

    • @synthesize age;

Member variables are accessed in the >setter and Getter implementations

> If the member variable age does not exist, it will automatically generate a @private member variable age

    • Manual implementation

> If the setter method is implemented manually, the compiler will only automatically generate getter methods

> If the Getter method is implemented manually, the compiler will only automatically generate setter methods

> If the setter and getter methods are manually implemented at the same time, the compiler does not automatically generate non-existent member variables

4. @property new features (haha, use this now)

    • Since Xcode 4.x, the @property has @synthesize functionality. In other words,@property can generate both a setter and a getter declaration and implementation

    • By default, implementations in the setter and Getter methods go to the member variables that begin with the underscore _

iOS development OC (vii)[email protected] and @synthesize

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.