The difference between Assign,copy,retain in iOS and the difference between weak and strong

Source: Internet
Author: User

@property (nonatomic, assign) NSString *title;

What is the difference between assign,copy,retain?

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

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.

There may be an example to describe it as appropriate.

Imagine our object is a dog, the dog wants to run Away (be released).

The strong type pointer is like a tethered dog. As long as you hang the dog with a leash, the dog will not run away. If there are 5 people holding a dog (5 strong pointers pointing to 1 objects), the dog will not run away unless 5 of the ropes fall off.

The weak type pointer is like a child pointing at the dog and shouting: "Look!" A dog is there. "As long as the dog is kept tied, the child can see the dog, and the (weak pointer) will always point to it. As long as the dog's leash comes off, the dog will run away, no matter how many children are watching it.

As long as the last strong pointer no longer points to the object, the object is freed and all weak pointers are cleared.

Use assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)

Using copy: On NSString

Use retain: For other nsobject and its subclasses

Nonatomic Keywords:

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.

The difference between Assign,copy,retain in iOS and the difference between weak and strong

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.