Differences between copy, retain, assign, readonly, read write, and nonatomic in objective-C

Source: Internet
Author: User

Nonatomic: Non-atomic access, no lock when assigning values to attributes, multi-thread concurrent access will improve performance. If this attribute is not added, both access methods are atomic transaction access by default.

(Atomic is a thread protection technology used by objc. Basically, it is used to prevent reading by another thread when the write is not completed, resulting in data errors. This mechanism consumes system resources. Therefore, nonatomic is a good choice for small devices such as the iPhone, if multi-thread communication programming is not used .)

Assign: Simple assignment without changing the index count
Applicable to basic data types (such as nsinteger) and C data types (INT, float, double, Char, etc.)

Copy:Create an object with an index count of 1 and release the nsstring

Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
For other nsobject and its subclass

//--------------------------
After reading so much, you may be a little dizzy. Now we will demonstrate the actual code:

@ Property (nonatomic, assign) int number;
An attribute of the int type is defined here, so this int is a simple data type and can be considered as an atomic access. Therefore, we use nonatomic instead of reference counting, so we use assign. Applicable to All simple data types.

@ Property (nonatomic, copy) nsstring * mystring;
An nsstring attribute is defined here, and no atomic operation is required. Therefore, nonatomic is used.
Why copy instead of retain! If you assign a value to mystring, the original string is a variable string (nsmutablestring) object, and you use retain, when the original string changes, your mystring attribute will also change. I don't think you want to see this phenomenon. In practice, if the original string is nsstring, it will only be retained, and no copy will be copied.

@ Property (nonatomic, retain) uiview * myview;
An attribute of the uiview type is defined here, and no atomic operation is required. Therefore, nonatomic is used.
When assigning values to myview, the original uiview object retaincount will add 1

// Interface file
@ Interface myclass: nsobject
@ Property (nonatomic, assign) int number;
@ Property (nonatomic, copy) nsstring * mystring;
@ Property (nonatomic, retain) uiview * myview;
@ End

// Implementation file
@ Implementation myclass
@ Synthesize number;
@ Synthesize mystring;
@ Synthesize myview;

// Release the memory
-(Void) dealloc
{
[Mystring release]; // the attribute of copy must be release;
[Myview release]; // release is required for retain attributes;

[Super dealloc]; // returns the parent object
}

@ End

Assume that you have a piece of code to create a myclass object

Myclass * instance = [myclass alloc] init];

// Assign a value to number. There is nothing to say. This is the case for simple data types.
Instance. Number = 1;

// Create a variable string
Nsmutablestring * string = [nsmutablestring stringwithstring: @ "hello"];

Instance. mystring = string; // assign values to mystring

[String appendstring: @ "world! "]; // Append text to string

Nslog (@ "% @", string); // here the string has changed and the output is "Hello World !"

Nslog (@ "% @", instance. mystring); // output mystring, you will find that the output here is still "hello" because mystring has copied a copy before the string changes

Uiview * view = [[uiview alloc] init];
Nslog (@ "retaincount = % d", view. retaincount );
// Output reference count of the view, which is 1 at this time

Instance. myview = view; // assign values to the myview attribute

Nslog (@ "retaincount = % d", view. retaincount );
// Output the reference count of the view again, which is 2 at this time, because the view is retained once by myview.

[View release];
// Although the view is released by release, the object pointer of the uiview retained by myview is still valid even if the view is retained by myview.

[Instance release];

Http://mobiler.blog.51cto.com/4481732/788385

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.