The meaning and misunderstanding of @property @synthesize

Source: Internet
Author: User

The role of @property is to define properties and declare Getter,setter methods. ( Note: property is not a variable )

The role of @synthesize is to implement attributes, such as the Getter,setter method.

In the case of declaring a property, if you override the Setter,getter method, you need to define the unrecognized variable in @synthesize and use the accessor method of the property for the variable. Such as:

The. h file

@property (nonatomic,assign) Nsinteger age;

@property (nonatomic,retain) nsstring * name;

@property (nonatomic,copy) nsstring * BB;

In the. m file

If you do not add the following three lines, the report will not find the _AGE,_NAME,_BB variable error

@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];

}

Summary: Be sure to distinguish between attributes and variables, not confused. @synthesize the declared property = variable. This means that the Setter,getter method of the property acts on this variable.

The meaning and misunderstanding of @property @synthesize

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.