IOS Object Properties Detailed Introduction _ios

Source: Internet
Author: User
Tags shallow copy

IOS Object Properties

Some properties of the OC object:

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

Here are a few separate functions and differences:

retain, counter plus 1, (add a pointer to memory) corresponding to the release (counter-1) Setter method to release the old value of the parameter and then retain the new value, all implementations are in this order

-(void) Setbackview: (UIView *) Backview {

  if (_backview!= backview) {

    [_backview release];

    _backview = [Backview retain];
  }

  return _backview;
}

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

(A little deep copy shallow copy, light copy deep 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)

The behavior of what is performed after the copy and Mutablecopy calls depends on how the class itself nscopying and nsmutablecopying protocols are implemented.

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

weak, weak reference

Strong properties used to modify strong references;

@property (Strong) SomeClass * aobject; 
corresponding to the original 
@property (retain) SomeClass * aobject; and @property (copy) SomeClass * aobject; 

Weak is used to modify the properties of weak references;
@property (weak) SomeClass * aobject; 
corresponding to the original 
@property (assign) SomeClass * aobject; 

__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 being directed is released, the pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function that are automatically released when the function returns.

The difference between strong and weak

(weak and strong) are different when an object no longer has a pointer to a strong type that is released, even if there is a weak pointer pointing to it.

Once the last strong pointer leaves, the object will be freed 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-shaped pointer is like a tethered dog. The dog will not run away as long as you hang the dog with a leash. If there are 5 people holding a dog (5 strong 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!" There's a dog. "As long as the dog is tethered, the child can see the dog," (weak pointer) will always point at it. As long as the dog's leash falls 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 an object, the object is freed, and all weak pointers are cleared.

Assign, for simple type, counter not add, direct assignment, a pointer, a piece of address, setter method directly assign value, do not do 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 weak, the so-called unsafe is to refer to the situation of the pointer will be easy to use

setter = xxxx, declaring the object's Set method

getter = xxxx, declaring object's Get method

nonatomic, non-atomic operation, the system does not add code, running relatively fast, but data operations in the case of multithreading is relatively insecure

Atomic, Atomic operation, the compiler will add a lot of lock unlock code, data manipulation in the case of multithreading relatively safe

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

Thank you for reading, I hope to help you, thank you for your support for this site!

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.