@property? @property? Talk about what's behind him.

Source: Internet
Author: User

What modifiers can be followed @property?

1. Thread-safe aaomic, nonatomic

2. ReadOnly, ReadWrite of access rights

3. Memory management (ARC) Assign, strong, weak, copy

4. Memory management (MRC) Assign, retain, release

5. Specify the method name setter =, getter =

Around the head of some of the problems to think about ~

What is the difference between using the weak keyword and comparing assign?

First we need to understand what weak and assign are.

Weak

A weak reference simply refers to an object, but does not own the object. Both an object is held without a weak reference, as long as there is no strong reference to him, then he will be cleared to release

Assign

Used for non-pointer variables.

For the underlying data type (for example, Nsinteger, CGFloat) and the C data type (int, float, double, char) there is also an ID.

When are we going to use weak?

1. In arc, when a circular reference is possible, it is often resolved by letting one end use weak, for example: Delegate Proxy property

2. You have already made a strong reference to him, it is not necessary to use a strong reference once, weak is also used, custom Iboutlet control properties are generally used weak, of course, you can also use strong.

Different points of weak and assign:

Weak this trait indicates that the attribute defines a "non-owning relationship" (nonowning relationship). When you set a new value for this property, the setting method neither preserves the new value nor releases the old value. This trait is similar to assign, but the attribute value is also emptied (nil out) when the object referred to by the attribute is destroyed. Assign's "Setup method" only performs simple assignment for the scalar type, such as CGFloat or Nslnteger. Assign can use non-OC objects, and weak must be used for OC objects.

How do I use the Copy keyword?

1.NSString, Nsarray, Nsdictionary, and so on often use the Copy keyword because they have a corresponding mutable type: nsmutablestring, Nsmutablearray, nsmutabledictionary There may be assignments between them, to ensure that the string values in the object are not inadvertently changed, and that a copy should be made when setting a new property value.

2.block using copy is a legacy from the MRC, in the MRC, the block inside the method is in the stack area, and copy can be used to place it in the heap. It is ok to write in ARC: The same is true for block using copy or strong. , but it doesn't hurt to write copy, and it's always a reminder that the compiler has copied the block automatically. If you do not write copy, the caller of the class may forget or never know that the "compiler will automatically copy the Block", and they may copy the property value themselves before the call. This operation is redundant and inefficient.

@property? @property? Talk about what's behind him.

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.