Ios@synthesize and @property

Source: Internet
Author: User

1. First talk about @property, this is the key word after iOS6. After you declare a property with it, the compiler automatically generates a setter and getter method for you.

@property (nonatomic, retain) NSString *name;

The setter method is as follows:

-(void) SetName: (nsstring*) _name{
First, determine if it is consistent with the old object, and if it is inconsistent, assign the value.
Because if it is an object, the code in the if will cause an extreme situation: when the name of the retain is 1 o'clock, so that the set operation let the instance name early release, and do not reach the assignment purpose.
if (name! = _name) {
[Name release];
name = [_name retain];
}
}

Getter methods are as follows

-(NSString *) name

{

return _name;

}

2. @synthesize declared in. m

@synthesize name = _name;

This way self.name = otherstring and name = Otherstring can be compiled correctly, but the former invokes the setter method for a copy, which is not equivalent to Assign/weak.

[email protected] and @property

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.