Assign, copy, strong, weak, nonatomic understanding, assignnonatomic

Source: Internet
Author: User

Assign, copy, strong, weak, nonatomic understanding, assignnonatomic

For example:

NSString * houseOfMM = [[NSString alloc] initWithString: 'Mm's three-bedroom and two-bedroom '];

The above code executes the following two actions:
1. Allocate a block of memory on the heap to store @ 'Mm's three-bedroom and two-hall '. For example, the memory address is 0X1111 and the content is 'Mm's three-bedroom and two-hall ',
2. Allocate a piece of memory on the stack to store houseForWife. For example, if the address is 0, the content of XAAAA is naturally 0X1111.

See the following (assign, retain, copy ):
1. assign: NSString * myHouse = [houseOfMM assign];
In this case, myHouse and houseOfMM are identical, and the address is both 0 XAAAA and the content is 0X1111. That is, myHouse is just the alias of houseOfMM, and any operation is equivalent to another operation. Therefore, retainCount does not need to be added)
2. retain: NSString * myHouse = [houseOfMM retain];
At this time, the myHouse address is no longer 0 XAAAA, which may be 0 XAABB, but the content is still 0X1111. therefore, both myHouse and houseOfMM can manage the memory of the three-bedroom and two-bedroom installation. Therefore, retainCount needs to be increased by 1. (some of them are independent, and each has two keys in and out)
3. copy: NSString * myHouse = [houseOfMM copy];

At this time, a memory storage @ 'Mm's three-bedroom and two-bedroom 'will be re-opened on the stack, for example, 0X1122, with the content @ 'Mm's three-bedroom and two-bedroom ', at the same time, space will be allocated for myHouse on the stack, for example, address: 0 XAACC, content is 0X1122, so retainCount increases by 1 for myHouse to manage the memory segment 0X1122. (two sets of @ 'three rooms and two halls of the fan', with good conditions, separated, and one set of houses, so the key is one by one .)


When should I use assign? Of course, it's a broken house.
Basic Types (simple type, atomic type): NSInteger, CGPoint, CGFloat, C data type (int, float, double, char, etc)

When to use copy
Classes that contain mutable subclasses that can be deeply copied, such as NSArray, NSSet, NSDictionary, NSData, NSCharacterSet, NSIndexSet, and NSString
(Houses that can be copied in depth)
However, NSMutableArray cannot be used, and copy cannot be used for Mutable. Otherwise, initialization may fail. Remember

When to use retain
Other NSObject and its subclass objects are good (most of them)

Strong in ARC is equivalent to retain in non-ARC. When the ARC comes, it will be strong if you have another key.

From: http://wenku.baidu.com/view/ab6ed804bed5b9f3f90f1c7d.html

Weak: the attribute of the object variable introduced by ARC, which has one more function than assign. When the object disappears, the pointer is set to nil, avoiding the wild pointer (not a null pointer, is a pointer to the "junk" memory (unavailable memory)

Nonatomic
It indicates that accessors are not atomic operations, but by default, accessors are atomic operations. That is to say, in a multi-threaded environment, the parser provides a secure access to the attributes. The returned values obtained from the reader or the values set by the setter can be completed at one time, even other threads are accessing it. If you do not specify nonatomic, the resolved accessors retain and automatically release the returned values in the memory management environment. If nonatomic is specified, the accessors simply return this value.

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.