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 if multi-thread communication programming is not used on small devices such as iPhone .)
Assign: Simple assignment without changing the index count
Basic data types (such as nsinteger) and C data types (INT, float, double, Char, etc) Applicable to simple data types
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)IntNumber;
An attribute of the int type is defined here. The Int type is a simple data type and can be considered as an atomic access. Therefore, nonatomic is used,No reference count is required, so assign is used. 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) Int Number;
@ Property(Nonatomic,Copy) Nsstring* Mystring;
@ Property(Nonatomic,Retain)Uiview * Myview;
@ End
// Implementation file
@ ImplementationMyclass
@ SynthesizeNumber;
@ SynthesizeMystring;
@ SynthesizeMyview;
// Release the memory
-(Void)Dealloc
{
[MystringRelease];// The copy attribute must be release;
[MyviewRelease]; // Release is required for retain attributes;
[SuperDealloc]; // returns the parent object
}
@ End
Assume that you have a piece of code to create a myclass object
Myclass* Instance= [MyclassAlloc]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 = [nsmutablestringStringwithstring: @ "hello"];
Instance. mystring= String; // Assign values to mystring
[StringAppendstring: @ "world! "]; // Append text to string
Nslog (@ "% @", string ); // Here the string has changed,
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 is changed.
Uiview* View = [[uiviewAlloc]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.
[ViewRelease];
// 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.
[InstanceRelease];