ARC\MRC use of the property keyword

Source: Internet
Author: User

Before using the attribute keyword, you need to understand the role of the property keyword. In fact, "simply speaking ..." is to guide the bottom how to generate set, get method, if you do not define the set, get method, the system will be automatically generated for you, but how to generate based on these property keywords, the combination of different property keywords will determine the set, get method specific implementation, The realization is different, the function naturally also is different ...

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

ARC:

1, assign: Basic data type, enumeration, struct body and other non-OC object types.

2, Strong/retain: The use must meet two conditions, one must be OC object type, second, need to be strong reference can not ' casually ' release.

3, weak:

1) Others to create others have, you just borrow it, at any time may not be seen, but you can find when not seen.

2) must be used to meet two conditions, one must be OC object type, and the other is not directly owned by the object. such as Delegate,parentviewcontroller, to prevent circular references.

3) Weak has a big trick, that is, when the object is released, it is automatically set to nil. The principle of realization "simply ..." is that every strong maintains a weaktable, and when a strong object is assigned to a weak object, the Weaktable object is recorded in weak. When the strong is released, all the objects in the weaktable are taken out to be nil.

4, copy: for block. Classes for mutable types, such as NSString, Nsdictionary, Nsarray, are understood here (copy= immutable). After testing, it was found that when assigning the NSString object to the copy type of NSString, the address did not change the same as the shallow copy and strong effect, but the address changed when Nsmutablestring was assigned to the copy type NSString The equivalent of a deep copy here.

5, Readonly/readwrite: Read-only/readable/writable.

6, Atomic/nonatomic: Atomic operation/non-atomic, "Simple ..." is thread-safe, but this method to ensure that the thread security overhead is very large, if the conditions allow or in the appropriate place to lock the reliable.

7, Getter/setter: None of the above does not matter, write their own set, get method.

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

Mrc:

1, assign: Basic data type, enumeration, struct body and other non-OC object types. It can also be used for objects not directly owned by Delegate\parentviewcontroller, as a substitute for MRC under weak.

2, retain: The use must meet two conditions, one must be OC object type, second, need to be strong reference can not ' casually ' release.

3,unsafe_unretained:

1) Others to create others have, you just borrow it, at any time may be missing, and when not even a greeting to play. The difference from weak in arc is that it is not automatically set to nil.

2) must be used to meet two conditions, one must be OC object type, and the other is not directly owned by the object.

3) The only difference from assign is that it cannot be used for basic data types.

4, copy: for block. Classes for mutable types, such as NSString, Nsdictionary, Nsarray, are understood here (copy= immutable). After testing, it was found that when assigning the NSString object to the copy type of NSString, the address did not change the same as the shallow copy and strong effect, but the address changed when Nsmutablestring was assigned to the copy type NSString I want to be a deep copy here.

5, Readonly/readwrite: Read-only/readable/writable.

6, Atomic/nonatomic: Atomic operation/non-atomic, "simple ..." is whether multithreading security, but through this method to ensure that multithreading security overhead is very large, if the conditions allow or in the appropriate place to lock the reliable.

7, Getter/setter: None of the above does not matter, write their own set, get method.

ARC\MRC use of the property keyword

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.