"iOS interview Series-1" The meaning and difference of @property after assign,copy,retain,weak,strong in iOS (mandatory-must be mastered in detail)

Source: Internet
Author: User

Source: Network

Assign: normal (simple) assignment, commonly used for basic data types, common delegate design patterns, once to prevent circular references. Do not change the index count (Reference counting). For underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)

retain: Keep count, get ownership of object, reference count on original basis +1. Releases 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. We set a reference count for that memory, and when 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. Retain after Count plus one. Alloc after the count is 1,release will call Dealloc destroy this object. If retain, you need to release two times. It is usually necessary to assign a parameter to a member variable in method retain. The Nsarray object retain the object in any array (retain value plus one). When Nsarray is unloaded (Dealloc), all objects in the array are freed once (the retain value is reduced by one). Not only Nsarray, but any collection class (Collection Classes) performs a similar operation. such as nsdictionary, or even uinavigationcontroller.

copy: Create an object with an index count of 1 and then release the old object. Used to copy an object, a generic string with an immutable object in Copy,foundation uses the copy effect equivalent to retain, just a reference count of +1.


Autorelease principle:

Autorelease and scopes do not have any relationship!
A. Create a autorelease pool first
B. The object is generated from within this autorelease pool.
C. After the object is generated, call the Autorelease function, which simply marks the Autorelease pool and lets the pool remember to release the object in the future.
D. At the end of the program, the pool itself needs to be rerlease, and the pool will release each object marked as Autorelease once. If an object retain count is greater than 1 at this point, the object is still not destroyed.

Remember one point: If this object is your alloc or new, you need to call release. If you use Autorelease, release only once the retain has occurred (let retain count is always 1).

In Arc,

Strong(strong reference) tells the compiler to help us insert retain automatically,

A weak(weak reference) is a common assignment equivalent to a assign that manages memory manually.

--------------------------------------------------------------------------------------------------------------- ----------------------

Difference

The difference between assign and retain , assign is the direct assignment, which may cause problems in 1, 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.

Copy and Retain

copy is to create a new object that will be used when you do not want a and B to share a piece of memory. A and B each have their own memory,retain is to create a pointer, reference object Count plus 1. The Copy property represents the same contents as two objects, the new object retain count is 1, independent of the reference count of the old object, and the old object does not change. Copy reduces the object's dependency on the context. The Retain property indicates that two objects have the same address (creating a pointer, copy of the pointer) and, of course, the same content, the retain value of this object +1 that is, retain is a pointer copy, copy is a copy of the content. this refers to objects such as Nsstring,nsnumber and so on.

The difference between weak and strong :

The difference (weak and strong) is that when an object no longer has a strong type pointer pointing to it, it is freed, even if there is a weak pointer to it.

Once the last strong pointer is left, the object will be released and all remaining weak pointers will be cleared.

The difference between retain and strong : accidental discovery when dealing with a problem raised by the Block property of the strong declaration. In many tutorials, you'll see that when declaring a property, the strong or retain effect is the same (it looks like more developers tend to use strong). However, when declaring a block, using strong and retain will have very different effects. Strong will be equal to copy, and retain is equal to assign! Of course, the definition of block or should use copy (there are other areas to note, you can refer to this article: Ios:arc and non-arc using the Block property problem), because the non-arc under the copy block will be in the stack, arc block will be on the heap. (http://blog.csdn.net/lvxiangan/article/details/50729888 don't know the real fake)

Assign and weak difference: weak more than assign a function, when the object disappears automatically turn the pointer to nil. Assign is a pointer assignment, not a reference count operation, and if not set to nil after use, a wild pointer may be generated. When your pointer type member variable does not need to be strongly referenced, use assign and weak can, for example some delegate, otherwise use strong
The base type can only use Assign,assigin for non-OC objects, whereas weak must be used for OC objects

block and weak differences

__block can be used in either ARC or MRC mode, can be decorated with objects, and can also be decorated with basic data types.

__weak can only be used in Arc mode, can only be decorated with objects (nsstring), and cannot be decorated with basic data types (int).

The Block object can be re-assigned in the block, weak not.

Atomic is a thread-protection technique used by OBJC, basically to prevent the data from being read by another thread when the write is not completed. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.

"Understanding: Http://www.cnblogs.com/langtianya/p/3691035.html"

"iOS interview Series-1" The meaning and difference of @property after assign,copy,retain,weak,strong in iOS (mandatory-must be mastered in detail)

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.