OC language @property @synthesize and ID

Source: Internet
Author: User

One, @property @synthesize keywords

Note: These two keywords are compiler features that allow Xcode to automatically generate the declarations and implementations of getter and setter.

(i) @property keywords

@property keyword can automatically generate a declaration of the setter and getter method for a member variable

@property int age;

When the compilation encounters this line, it is automatically extended to the following two sentences:

-(void) Setage: (int) age;

-(int) age;

(ii) @synthesize keywords

@synthesize keyword helps generate the implementation of the setter and getter methods for member variables.

Syntax: @synthesize age=_age;

Equivalent to the following code:

-(void) Setage: (int) Age

{

_age=age;

}

-(int) age

{

Return _age;

}

(iii) Use and use of keywords note

The declarations section of the class:

The implementation part of the class:

Test procedure:

In the new version:

The declarations section of the class:

The implementation part of the class:

Test procedure:

(1) in the old code, @property can only be written in the @interface @end, @synthesize can only be written in the @implementation @end, since Xcode 4.4, @ The property is the function of @property and @synthesize.

(2) @property int age; 3 functions: 1) generate declarations of get and set methods for _age member variables, 2) Generate _age member variable set and get method implementations, 3) generate a _age member variable.

Note: The member variables generated in this way are private.

(3) You can add int _age to {}, and the declared _age is protected.

(4) Principle: Get and Set methods are the same as variables, if you define yourself, then use what you have defined, if not defined, then automatically generate a.

(5) Manual implementation:

1) If the Set method is implemented manually, then the compiler generates only the get methods and member variables;

2) If the Get method is implemented manually, then the compiler generates only the set method and member variable;

3) If both the set and get methods are implemented manually, the compiler will not generate member variables.

Second, Id

An ID is a type, universal pointer that can point to \ manipulate any object.

Note: In the definition of ID, the * number is already wrapped. The ID pointer can only point to an OS object.

Definition of ID Type

Typedef struct OBJC object{

Class Isa;

} *id;

Limitations: Call a nonexistent method, the compiler will immediately error.

OC language @property @synthesize and ID

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.