iOS Object Properties Detailed

Source: Internet
Author: User

Some properties of the OC object:

Retain,strong, Copy,weak,assign,readonly, ReadWrite, unsafe_unretained

Here are the separate functions and differences:

retain, counter plus 1, (add a pointer to the memory) corresponding to the release (counter-1) Setter method to release the old value of the parameters and then retain the new value, all the implementation is this order

-(void) Setbackview: (UIView *) Backview {    if (_backview! = Backview) {        [_backview release];        _backview = [Backview retain];    }    return _backview;}

Copy, copy, new open address, content copy, setter method copy operation, and retain processing process, the first old value release, and then copy the new object, Retaincount is 1. This is the mechanism introduced to reduce dependency on the context.

(Make a bit of deep copy shallow copy, shallow copy of deeper copy)

This article is more intuitive (http://blog.csdn.net/omegayy/article/details/7311839)

Official documents (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html)

How the behavior after copy and Mutablecopy calls behaves depends on how the class itself nscopying and nsmutablecopying protocols are implemented.

Strong, strong reference, counter plus 1, same as retain (corresponds to retain and copy)

weak, weak references

__weak, __strong is used to modify variables, in addition to __unsafe_unretained, __autoreleasing are used to modify variables.
__strong is the default keyword.
__weak declares a weak reference that can be automatically nil.
__unsafe_unretained declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function, which is automatically released when the function returns.

The difference between strong and weak

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.

assign, for simple type, counter not added, direct assignment, a pointer, a piece of address, setter method directly assign value, do not perform any retain operation, in order to solve the original type and circular reference problem

readonly, read only, generate Get method, no set method

readwrite, default, read-write, Set,get method will be generated

unsafe_unretained, similar to the weak, the so-called unsafe refers to the situation will be easy to appear also pointers, with caution

setter = xxxx, declaring object's Set method

getter = xxxx, declaring the object's Get method

nonatomic, non-atomic operation, the system does not add code, running relatively fast, but the data operation in the multi-threaded situation is relatively insecure

Atomic, atomic operations, compile will add a lot of lock unlock code, data manipulation in multi-threaded case relatively safe

* Using assign: for underlying data types (Nsinteger) and C data types (int, float, double, char, etc.)
* Use copy: for NSString
* Use retain: For other nsobject and its subclasses

iOS Object Properties Detailed

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.