Assign,retain,copy,atomic,nonatomic,readonly,readwrite and Strong,weak in the Object-c

Source: Internet
Author: User
Tags scalar

Assign: Specifies the setter method with a simple assignment, which is the default action. You can use this property for scalar types, such as int. You can imagine a float, which is not an object, so it can't be retain, copy. Assign specifies the setter method with a simple assignment, which is the default action. You can use this property for scalar types, such as int. You can imagine a float, which is not an object, so it can't be retain, copy.

retain: Specifies that the retain should be called on a later object, and the previous value sends a release message. You can imagine a nsstring instance, which is an object, and you might want to retain it. Retain frees the old object, assigns the value of the old object to the input object, and then increases the input object's index count to 1, using retain: For the other nsobject and its subclasses, retain, is to describe the property at the time of assignment, before releasing the value before releasing the new value to the property, Reference plus 1.

Copy: Specifies that a copy of the object (deep copy) should be used, and the previous value sends a release message. Basically like retain, but without increasing the reference count, it is allocating a new piece of memory to place it.

readonly: Only getter methods will be generated and no setter method is generated (getter methods do not have a get prefix).

readwrite: The default property will generate getter and setter methods with no extra parameters (the setter method has only one parameter).

Atomic: The default property for an object is that the Setter/getter generated method is an atomic operation. If there are multiple threads calling the setter at the same time, there will not be one thread executing the setter all the way before the other thread starts executing the setter, related to the method's tail and end lock.

nonatomic: The atomicity of Setter/getter is not guaranteed, and data may be problematic in multithreaded situations.


Copy: Create an object that has a reference count of 1 and then release the old object

Retain: Frees the old object, assigns the value of the old object to the input object, and then increases the reference count of the input object to 1

Copy actually creates an identical object, and retain is not:
such as a NSString object, the address is 0x1111, the content is @ "STR"
Copy to another nsstring, the address is 0x2222, the content is the same, the new object retain is 1, the old object has not changed

Retain to another nsstring, the same address (set up a pointer, pointer copy), the content of course the same, the object's retain value +1

That is, retain is a pointer copy and copy is a copy of the content. The old object will be released before the copy.

1. Using copy: To NSString

2. Use retain: For other nsobject and its subclasses

Strong and weak are the new object variable properties introduced by arc

ARC introduces a new life-cycle qualification for new objects, which is 0 weak references. If the object that the 0 weak reference points to is deallocated, the 0 weakly referenced object is automatically set to nil.



@property (strong) MyClass *myobject;

Equivalent to @property (retain) MyClass *myobject;

@property (weak) Myotherclass *delegate;

Equivalent to @property (assign) Myotherclass *delegate;
The generalized difference between a strong reference and a weak reference:
The survival of the object is directly determined by the existence of strong references, which are often referred to as references. If a reference to an object does not exist, and the object no longer appears in the list, the object is freed from memory.
Weak references are the same as strong references, except that they do not determine the survival of the object. Even if an object is held by an infinite number of references, it will be cleared if no strong reference is directed to him.



Simply speaking strong equals retain
Weak more than assign a function, when the object disappears automatically turn the pointer to nil, the benefits are self-evident.



__weak, __strong is used to modify variables, in addition to __unsafe_unretained, __autoreleasing are used to modify variables.
__strong is the default keyword.
__weak declares a weak reference that can be automatically nil.
__unsafe_unretained declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function, which is automatically released when the function returns.

1. Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a value to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.

2. Understand the problem of assign in 1, then how to solve? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.

3. Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled.
4. Copy is used when you do not want a and B to share a piece of memory. A and B each have their own memory.
5. Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:14PX;" >if (Property! = newvalue) {
    2. [Property release];
    3. property = [NewValue retain];
    4. }</span>
[CPP]View Plaincopy

About retain,copy,assign The difference problem actually troubled me for a long time, because in the program is not too often used to copy,assign, so the specific differences of the three have not quite understood.

According to my understanding, assign and retain difference, is introduced a counter retaincount, can be a memory of the release of a lot of convenience. Copy, is to copy the original memory again, so that each has a memory, so that the release of the time will not be wrong. Assign: Simple assignment, without changing the index count (Reference counting). Copy: Create an object with an index count of 1 and then release the old object retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1 using assign: For the underlying data type (Nsinteger, CGFloat) and C data types (int, float, double, char, and so on) use copy: for nsstring use retain: nonatomic for other nsobject and its subclasses, non-atomic access, no synchronization, Multithreaded concurrent access can improve performance. Note that if you do not add this property, the default is two access methods are atomic transaction access [CPP]View Plaincopy
  1. @property (nonatomic, retain) Uitextfield *username //compile-time auto-generated code
  2. -(Uitextfield *) UserName {
  3. return userName;
  4. }
  5. -(void) Setusername: (Uitextfield *) username_ {
  6. [UserName release];
  7. UserName = [username_ retain];
  8. }
  9. @property (retain) Uitextfield *username //auto-generated code
  10. -(Uitextfield *) UserName {
  11. Uitextfield *retval = nil;
  12. @synchronized (self) {
  13. retval = [[UserName retain] autorelease];
  14. }
  15. return retval;
  16. }
  17. -(void) Setusername: (Uitextfield *) username_ {
  18. @synchronized (self) {
  19. [UserName release];
  20. UserName = [username_ retain];
  21. }
  22. }

Assign,retain,copy,atomic,nonatomic,readonly,readwrite and Strong,weak in the Object-c

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.