Explanation of iOS Object Attributes

Source: Internet
Author: User

Explanation of iOS Object Attributes
Some attributes of the oc object: retain, strong, copy, weak, assign, readonly, readwrite, unsafe_unretained. The following describes their respective functions and differences: retain, counter plus 1, (add a pointer to the memory) corresponding to the release (counter-1) setter method to perform the release old value for the parameter and then retain the new value. All implementations copy the code in this order-(void) setBackView :( UIView *) backView {if (_ backView! = BackView) {[_ backView release]; _ backView = [backView retain];} return _ backView;} copy the code copy, copy, open address, and copy the content, the setter method performs the Copy operation. Like the retain processing process, the old value is release first, and then the new object is copied. retainCount is 1. This is a mechanism introduced to reduce context dependencies. This article is more intuitive (http://blog.csdn.net/omegayy/article/details/7311839) official documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html) what is the behavior after the copy and mutableCopy calls is completed depends on how the NSCopying and NSMutableCopying protocols of the class are implemented. Strong, strong reference, counter plus 1, same as retain (corresponding to retain and copy) weak, weak reference copy code strong is used to modify strong reference attributes; @ property (strong) someClass * aObject; corresponding to the original @ property (retain) SomeClass * aObject; and @ property (copy) SomeClass * aObject; weak is used to modify attributes of weak references; @ property (weak) someClass * aObject; corresponding to the original @ property (assign) SomeClass * aObject; copy the code _ weak, _ strong to modify the variable, and _ unsafe_unretained, _ autoreleasing is 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 it will not be automatically nil. That is to say, if the memory area to which it is directed is released, this pointer is a wild pointer. _ Autoreleasing is used to modify a function parameter, which is automatically released when the function returns. The difference between strong and weak (weak and strong) is that when an object no longer has a pointer of the strong type pointing to it, it will be released, even if there is a weak pointer pointing to it. Once the last strong pointer leaves, the object will be released and all remaining weak pointers will be cleared. It may be appropriate to give an example. Imagine our object is a dog, and the dog wants to run away (be released ). The strong pointer is like a dog. The dog will not run away as long as you hold the dog with a rope. If five people hold a dog (five strong pointers point to one object), the dog won't run unless the Five ropes fall off. The weak pointer is like a child pointing to a dog and shouting, "Look! A dog is there. "As long as the dog is down, the child will be able to see the dog, which will always point to it. As long as the dog's rope 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 the object, the object will be released and all weak pointers will be cleared. Assign is used for simple type. The counter is not added. It is directly assigned a value, a pointer, an address, and a setter method. No retain operation is performed, to solve the problem of original type and loop reference readonly, read-only, only get method is generated, no set method readwrite, default, read/write, set, get method will generate unsafe_unretained, similar to weak, the so-called unSafe refers to the situation where pointers are also prone. Use setter = xxxx with caution, declare the object's set method getter = xxxx, declare the object's get method nonatomic, and perform non-atomic operations, the system does not add code and runs at a relatively high speed. However, in the case of multithreading, data operations are relatively insecure, such as atomic and atomic operations. During compilation, many locks are added to unlock the code, data operations are relatively secure in the case of multithreading * use assign: Use copy for basic data types (NSInteger) and C data types (int, float, double, char, etc: use retain for NSString *: for other NSObject and its subclass

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.