IOS @ synthesize and @ property, iossynthesize

Source: Internet
Author: User

IOS @ synthesize and @ property, iossynthesize

1. First, let's talk about @ property, which is a keyword coming out after iOS6. After declaring an attribute with it, the compiler will automatically generate the setter and getter methods for you.

@ Property (nonatomic, retain) NSString * name;

The setter method is as follows:

-(Void) setName :( NSString *) _ name {
// First, determine whether it is consistent with the old object. If it is inconsistent, assign a value.
// If it is an object, executing the code in if will cause an extreme situation: When the retain of this name is 1, this set operation allows the Instance name to be released in advance without assigning values.
If (name! = _ Name ){
[Name release];
Name = [_ name retain];
}
}

The getter method is as follows:

-(NSString *) name

{

Return _ name;

}

2. @ synthesize stated in. m

@ Synthesize name = _ name;

In this way, both self. name = otherString and name = otherString can be compiled normally, but the former calls the setter Method for a copy, and the latter does not, which is equivalent to assign/weak.

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.