Introduction to attributes of @ property in iOS development

Source: Internet
Author: User

We all know that @ property and @ synthesize can automatically generate an access method for a class member variable. However, they may not be very familiar with some properties in property, and some on the Internet are not very correct, it may mislead new users, so I want to introduce the detailed attributes in property in detail.

First, we will introduce the default situation:

Readwrite: This attribute is the default and will automatically generate an accesser for you.

Assign: This attribute is generally used to process basic types, such as int and float. If the declared attribute is of the basic type, assign is the default type. You can skip this attribute.

For assign, its accessorsCodeYes:

 
@ Property (nonatomic, assign) nsstring * myfield-(nsstring *) myfield {return myfield;}-(void) setmyfield: (nsstring *) newvalue {myfield = newvalue ;}
 
 

Atomic: This attribute is available by default to ensureProgramIn the case of multithreading, the compiler will automatically generate some mutex lock code to avoid the problem of non-synchronization of read/write of the variable.

Then let's talk about other situations:

Readonly: Only getter generation does not have the setter Method

Copy: this will automatically generate the clone of the assigned object, which is equivalent to generating a new copy of the object in the memory. As a result, changing the assigned object will not change the member variable you declared.

Retain: Automatically assigns a value to the retain object. The specific implementation is as follows:

 
@ Property (nonatomic, retain) nsstring * myfield-(nsstring *) myfield {return myfield;}-(void) setmyfield: (nsstring *) newvalue {If (newvalue! = Myfield) {[myfield release]; myfield = [newvalue retain] ;}}

Therefore, you must first determine whether the current myfield is the object assigned with a new value. If you do not need to release yourself, the values and retain will be assigned.

Nonatomic: if you do not need to consider multithreading for this object, add this attribute so that the compiler will generate less mutex lock code, which can improve efficiency.

Related Article

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.