Atomic nonatomic assign retain copy strong weak introduction

Source: Internet
Author: User

Atomic and nonatomic are used to determine whether the compiler-generated getter and setter are atomic operations.

Atomic

When you set the @property property of a member variable, the default is atomic, which provides multithreading security.

In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:
{Lock}
if (Property! = newvalue) {
[Property release];
property = [NewValue retain];
}
{Unlock}

Nonatomic

Prohibit multi-threading, variable protection, improve performance.

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.

Indicates that the accessor is not an atomic operation, and the accessor is an atomic operation by default. This means that, in a multithreaded environment, the parsed accessor provides a secure access to the property, the return value obtained from the picker, or the value set by the setting can be done at once, even if another thread is accessing it. If you do not specify nonatomic, the parsed accessor retains and automatically releases the returned value in the environment in which it manages the memory, and if nonatomic is specified, the accessor simply returns the value.

Assign
For the underlying data type (nsinteger,cgfloat) and the C data type (int, float, double, char), and so on.
This flag indicates that the setting is assigned directly, which is also the default value. In an application that uses garbage collection, if you want a property to use assign, and this class conforms to the nscopying protocol, you should explicitly indicate this tag instead of simply using the default value, otherwise you will get a compile warning. This again explains to the compiler that you really need to assign a value, even if it is a copy.

Retain
Release old values of parameters to other nsobject and their subclasses, and then retain new values
Specifies that retain will wake the retain message of the incoming value when the value is assigned. This property can be used only for Objective-c object types, not for core Foundation objects. (for obvious reasons, retain increases the object's reference count, and neither the base data type nor the core Foundation object has a reference count-the translator's note).

Note: When you add an object to an array, the reference count increases the number of references to the object by +1.

Copy
For NSString it states that a copy of the passed-in value is used when the value is assigned. Copy work is performed by the copy method, and this property is valid only for those object types that implement the Nscopying protocol. For a more in-depth discussion, refer to the "Copying" section.

Copy and retain:

Copy actually creates an identical object, and retain is not:
1. For example a NSString object, the address is 0x1111, the content is @ "STR", Copy to another nsstring after the address is 0x2222, the content is the same.

2. The new object retain is 1, the old object has not changed retain to another nsstring after the same address (set up a pointer, pointer copy), the contents of course the same, the object's retain value +1.
Summary: Retain is a pointer copy and copy is a copy of the content.

Assign and retain:

1. Contact C, then assume that 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.
Summary: The above two is actually the difference between assign and retain, assign is the direct assignment, which may cause 1 of the problem, 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.

-------------------Strong Weak

Strong and weak are the new object variable properties introduced by Arc, which introduces new life-cycle qualifiers for new objects, or 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; a 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. No way, or "strong brother" have face. 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.

Before writing a geo-location-based application, the application cannot be opened for positioning, and in the. h file, the attributes I define are:

@property (Weak, nonatomic) Cllocationmanager *locationmanager;

Later, I changed the above sentence to read:
@property (nonatomic, retain) Cllocationmanager *locationmanager;

You can do it.

Atomic nonatomic assign retain copy strong weak introduction

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.