Copy: Create an object with an index count of 1 and release the nsstring
It indicates to nsstring that a copy of the input value is used when the value is assigned. The copy operation is executed by the copy method. This attribute is only valid for object types that implement the nscopying protocol. For more information, see "copy.
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
Perform the release old value for the parameter, and then retain the new value.
The specified retain will wake up the retain message of the input value when the value is assigned. This attribute can only be used for the objective-C object type, but not for the core foundation object. (The reason is obvious: retain will increase the reference count of the object, and neither the basic data type nor the core foundation object has reference count-Translator's note ).
Note: when an object is added to an array, the reference count increases the number of object references by more than 1.
The actual retain syntax is:
-(Void) setname :( nsstring *) newname {
If (name! = Newname ){
[Name Release];
Name = [newname retain];
// Name's retain count has been bumped up by 1
}
}
Perform the release old value for the parameter, and then retain the new value.
The specified retain will wake up the retain message of the input value when the value is assigned. This attribute can only be used for the objective-C object type, but not for the core foundation object. (The reason is obvious: retain will increase the reference count of the object, and neither the basic data type nor the core foundation object has reference count-Translator's note ).
Note: when an object is added to an array, the reference count increases the number of object references by more than 1.
The actual retain syntax is:
-(Void) setname :( nsstring *) newname {
If (name! = Newname ){
[Name Release];
Name = [newname retain];
// Name's retain count has been bumped up by 1
}
}
Copy and retain:
Copy actually creates the same object, and retain is not:
For example, an nsstring object with the address 0 × 1111 and the content @ "STR"
After copying data to another nsstring, the address is 0 × 2222, the content is the same, the new object retain is 1, and the old object does not change
After retain reaches another nsstring, the address is the same (create a pointer and copy the pointer), and the content is of course the same. The retain value of this object is + 1
That is to say, retain is a pointer copy and copy is a content copy. Wow, it's much easier than you think...
Retain's set method should be light replication, and the copy's set method should be deep replication.
Another usage of copy:
Copy is a copy of the content, which is true for nsstring.
But what if we copy an nsarray? For example,
Nsarray * array = [nsarray arraywithobjects: @ "hello", @ "world", @ "baby"];
Nsarray * array2 = [array copy];
At this time, the system indeed opened up a memory space for array2, but we need to realize that each element in array2 only copies the pointer to the corresponding element in the array. this is the so-called "Shallow replication ".
Assign: simple assignment without changing the index count
Applicable to basic data types (such as nsinteger, cgfloat) and C data types (INT, float, double, Char, etc.)
This flag indicates that the setter directly assigns a value, which is also the default value. In applications that use garbage collection, if you want an attribute to use assign and the class complies with the nscopying agreement, you need to specify this tag rather than simply using the default value, otherwise, you will get a compilation warning. This again shows to the compiler that you really need to assign values, even if it is copyable.
Assign and retain:
1. if you have been in contact with C, assume that you have allocated a piece of memory with malloc and assigned its address to pointer A. Later, you want pointer B to share the same piece of memory, so you assigned a to (assign) B again. At this time, a and B point to the same memory. Can a directly release this memory when a no longer needs it? The answer is no, because a does not know whether B is still using this memory. If a is released, B will cause crash when using this memory.
2. I learned about the assign problem in 1. How can I solve it? The simplest way is to use reference counting. In the example above, we set a reference count for the memory. When the memory is allocated and assigned to, the reference count is 1. When a is assigned to B, the reference count is increased to 2. If a no longer uses this memory, it only needs to reduce the reference count by 1, indicating that it no longer owns this memory. When B no longer uses this memory, it also reduces the reference count by 1. When the reference count is 0, it indicates that the memory is no longer referenced by any pointer, and the system can release it directly.
Conclusion: The above two points are actually the differences between assign and retain. Assign is a direct value assignment, which may cause problems in 1. When the data is of native types such as int and float, assign can be used. As described in section 2, retain uses the reference count. Retain causes the reference count to increase by 1, and release causes the reference count to decrease by 1. When the reference count is 0, the dealloc function is called, memory is recycled.
Nsstring * PT = [[nsstring alloc] initwithstring: @ "ABC"];
The above code executes the following two actions:
1. Allocate a block of memory on the heap to store @ "ABC". For example, if the memory address is 0x1111, the content is "ABC"
2. Allocate a block of memory on the stack to store pt. For example, the address is 0 xaaaa and the content is naturally 0x1111.
Next, let's take a look at assign retain copy.
Assign: nsstring * newpt = [PT assing];
At this time, the addresses of newpt and PT are completely the same. The content of 0 xaaaa is 0x1111, that is, newpt is only the alias of PT, and any operation is equivalent to another operation. Therefore, retaincount does not need to be added.
Retain: nsstring * newpt = [PT retain];
At this time, the newpt address is no longer 0 xaaaa, which may be 0 xaabb but the content is still 0x1111. Therefore, both newpt and Pt can manage the memory where "ABC" is located. Therefore, retaincount needs to be increased by 1.
Copy: nsstring * newpt = [PT copy];
In this case, a memory segment will be re-opened on the stack to store @ "ABC". For example, if 0x1122 is @ "ABC, a space such as the address will be allocated to newpt on the stack: the content of 0 xaacc is 0x1122. Therefore, retaincount is increased by 1 for newpt to manage the memory segment 0x1122.
Readonly indicates that the attribute is read-only and the default tag is read/write. If read-only is specified, only one reader is required in @ implementation. Or if you use the @ synthesize keyword, the reader method is parsed. And if you try to assign values to attributes using the vertex operator, you will get a compilation error.
Readwrite indicates that the attribute is read/write, which is also the default attribute. Both the configurator and reader must be implemented in @ implementation. If the @ synthesize keyword is used, the reader and the setter are parsed.
Nonatomic: Non-atomic access without locking when assigning values to attributes. multi-thread concurrent access improves performance. If this attribute is not added, both access methods are atomic transaction access by default.
Above from http://hi.baidu.com/wolf_childer/item/3a0f1affb238a11ce2e3bdb0
Weak and strong property (difference between strong reference and weak reference)
The weak and strong attributes are required only when you open the Arc. In this case, you cannot use the retain release autorelease operation because the arc will automatically perform these operations for you, however, you need to use weak and strong on the object attributes, where strong is equivalent to the retain attribute, while weak is equivalent to assign.
You only need to use weak (strong by default) to avoid retain cycles (that is, the parent class contains subclass {parent class retain's subclass }, the sub-class also calls the parent class {sub-class and retain the parent class}, so it cannot be release)
ARC is a new feature launched by iOS 5, which is called Automation Reference counting ). Simply put, retain/release is automatically added to the Code. The Code that was manually added to handle the reference count of memory management can be automatically completed by the compiler.
This function starts importing iOS 5/Mac OS X 10.7 and can be used after xcode4.2.
Strong and weak in property
The strong keyword is similar to the retain keyword. When it is used, the reference count is automatically + 1, and the instance can better describe everything.
@ Property (nonatomic, strong) nsstring * string1;
@ Property (nonatomic, strong) nsstring * string2;
There are two attributes,
@ Synthesize string1;
@ Synthesize string2;
Guess what results will the following code output?
Self. string1 = @ "string 1 ";
Self. string2 = self. string1;
Self. string1 = nil;
Nslog (@ "string 2 = % @", self. string2 );
Result: String 2 = string 1
Because string2 is a property defined by strong, reference count + 1 makes them all point to @ "string 1". If you are familiar with retain, This is not difficult to understand.
Next let's look at the keyword weak:
If two attributes are declared as follows:
@ Property (nonatomic, strong) nsstring * string1;
@ Property (nonatomic, weak) nsstring * string2;
And define
@ Synthesize string1;
@ Synthesize string2;
What is the output below?
Self. string1 = @ "string 1 ";
Self. string2 = self. string1;
Self. string1 = nil;
Nslog (@ "string 2 = % @", self. string2 );
Result: String 2 = NULL
For analysis, string1 is nil because self. string1 and self. string2 point to the same address and string2 does not have a retain memory address, while self. string1 = nil releases the memory. The pointer declared as weak. Once the address pointed to by the pointer is released, these pointers will be assigned as nil. This benefit effectively prevents wild pointers. In the C/C ++ development process, why do Daniel say that when the pointer space is released, the pointer should be assigned null. here we use the weak keyword to help us with this step.
Above from http://www.cnblogs.com/mybkn/archive/2012/03/08/2384860.html
There are other issues regarding the use of retain, strong, or weak for some UI properties.
Retain, strong, or weak should be used for some UI properties.
1. What I saw in the Stanford University video is that using weak, ios5 will help you do everything else, and even release will be useless, dealloc does not need to be reloaded (maybe I didn't think so seriously, it seems like this ).
2. Retain is often seen in the company's project code.
3. If @ property is used for declaration, do you still need to declare the instance variables (two curly braces under the interface ). The Stanford University video didn't seem to see the use of instance variables (currently the third one ).
4. Does the popular iPhone 4 and iPhone 4S currently use ios4? To ensure compatibility, should I not use the new features of ios5.
1. For retain, it generally refers to pointers. These attributes need to save the reference count to prevent botnets. At that time, for nsstring type, this is not retain, but copy, however, for strong, you do not need to consider this. It will determine whether to choose retain or copy. For assign, it is a non-pointer variable, such as nsinteger, in addition, to avoid loop reference, weak is similar to assign, but it is a little more, that is, it will automatically set this type of variable to nil.
2. As for the reasons why companies often use retain, one is due to coding habits, and the other is based on project needs.
3. Whether to declare instance variables depends on special circumstances. If you do not declare instance variables in the interface, but you are. in the M file, @ synthesize indicates that you have declared the instance variable.
4. The system can be upgraded. There is no rule that only ios4 can be used for iPhone 4S.
1. For details, iboutlet can be weak, nsstring is copy, and delegate is generally weak. Generally, the "internal" attribute of the class is set to strong, and the "external" attribute of the class is set to weak. In the end, it is a matter of authority. The memory cannot be released due to cyclic reference.
2. There will be a lot of retian without using arc.
3. If you write @ synthesize abc = _ ABC;, the system automatically declares an _ ABC instance variable.
Use assign: for basic data types (nsinteger) and C data types (INT, float, double, Char, etc)
Use copy: To nsstring
Use retain: for other nsobject and its subclass
From http://www.cocoachina.com/bbs/simple? T1102.16.html
Copy, retain, assign, readonly, readwrite, strong, weak, nonatomic sorting