The difference between Nonatomic,assign,copy,retain in objective-c

Source: Internet
Author: User

Nonatomic: Non-atomic access, non-synchronous, multi-threaded concurrent access improves performance. If this attribute is not added, the default is two access methods are atomic transaction access.
(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. )
Assign: Simple assignment, not changing index count
For the underlying data type (Nsinteger) and the C data type (int, float, double, char, and so on),Failure to do so may result in memory leaks. For example, if you use malloc to allocate a piece of memory and assign its address to pointer A, then if you want pointer b to share this memory, then say a assigns to (Assgin) b. At this point the assgin is used, and a and B point to the same piece of memory. But now the problem arises, and when a no longer needs this memory, can it be released directly? Must not be, because a does not know whether B is still using this memory, if a is released, then B in the use of this memory when the program crash out.


Copy: Create an object with an index count of 1 and then release the old object
To NSString

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
For other NSObject and its subclasses

The difference between copy and retain:
Copy is to create a new object, retain is to create a pointer to reference the object Count plus 1.
Eg: an NSString object with an address of 0x1111 with the content @ "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.


More code Examples

The difference between Nonatomic,assign,copy,retain in objective-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.