Differences between copy, tetain, assign, readonly, readwrite, 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.CodeDemo:

@ Property(Nonatomic,Assign)IntNumber;
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
@ InterfaceMyclass:Nsobject
@ Property(Nonatomic,Assign)IntNumber;
@ Property(Nonatomic,Copy)Nsstring* Mystring;
@ Property(Nonatomic,Retain)Uiview* Myview;
@ End

// Implementation file
@ ImplementationMyclass
@ SynthesizeNumber;
@ SynthesizeMystring;
@ SynthesizeMyview;

// Release the memory
-(Void)Dealloc
{
[Mystring Release]; // The copy attribute requires 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 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];

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.