@ Property @ synthesize: Meaning and misunderstanding.

Source: Internet
Author: User
@ Property defines attributes and declares the getter and setter methods. (Note: The property is not a variable)
@ Synthesize is used to implement attributes, such as the getter and setter methods.

If you override setter, Getter, and method when declaring an attribute, You need to define the unrecognized variable in @ synthesize and apply the attribute access method to the variable. For example:

. H file

@property (nonatomic,assign)NSInteger age;@property (nonatomic,retain)NSString * name;@property (nonatomic,copy)NSString * bb;

In the. M file

// If the following three rows are not added, the system reports that the _ age, _ name, And _ BB variables cannot be found. @ synthesize age = _ age; @ synthesize name = _ name; @ synthesize BB = _ BB;-(void) setname :( nsstring *) name {If (_ name! = Name) {[_ name release]; _ name = [name retain] ;}}- (nsstring *) name {return [[_ name retain] autorelease];} -(void) setage :( nsinteger) Age {_ age = age;}-(nsinteger) Age {return _ age;}-(void) setbb :( nsstring *) bb {If (_ BB! = Bb) {[_ BB release]; _ BB = [BB copy] ;}}- (nsstring *) bb {return [[_ BB retain] autorelease];}

?

Conclusion: The difference between attributes and variables must be distinguished and cannot be confused. @ Synthesize the declared attribute = variable. It means to apply the setter and getter methods of the attribute to this variable.

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.