After analyzing attributes such as nomantic, copy, and retain, in the new XCode version, we often see the following four attributes: _ unsafe_unretain, _ strong, _ weak, and _ autoreleasing. What are their functions?
_ Unsafe_unretain, _ strong, _ weak, and _ autoreleasing appear after LLVM compiler 3.0. While _ unsafe_unretain, _ strong, and _ autoreleasing are available without using ARC (automatic reference count. In ARC, the default pointer is the _ strong attribute. This means that an object is assigned to another pointer. As long as the pointer references this object, the object will remain. This is useful for most objects, but this may cause retain cycle. For example, if you have an object that contains another instance variable object, but the second object uses the previous object as its delegate, the two objects will not be released.
Because of the above reasons, the _ unsafe_unretain and _ weak qualifiers exist. They are usually used to modify delegate, that is, when defining a delegate attribute, use _ unsafe_unretain and _ weak to modify it, then, the instance variables are marked separately by using _ unsafe_unretain and _ weak. This means that the delegate instance variable will still be able to point to the first object, but it will not result in retaining the first object, so it breaks the retain cycle and can release two objects
In addition to delegate, __unsafe_unretain and _ weak modifiers, you can also avoid retain cycle in your code. Leaks instrument now contains a cycle view, which allows you to find the retain cycle in your application and display the image.
Both _ unsafe_unretain and _ weak can avoid retain cycle, but they are also slightly different. For _ weak, when the pointer is released to the object, the pointer of the object is converted to nil, Which is safer. And _ unsafe_unretain, as the hidden meaning of its name, even though the release Pointer Points to the object, the pointer will continue to point to the original memory. This will cause the application crash, so it is unsafe.
Why do we still need to use _ unsafe_unretain? This is because _ weak does not appear until iOS5.0 and lion.
The English description of _ autoreleasing is as follows: to denote arguments that are passed by reference (id *) and are autoreleased on return, which is mainly used for reference parameter passing.
Note that cocoa sets a rule, that is, the parent object creates a strong reference to the sub-object, while the sub-object only creates a weak reference to the parent object.
When using weak references, you must note that when you send a message to a dealloc weak reference object, your program will crash. Therefore, you must carefully determine whether the object is valid. In most cases, objects with weak references know the weak references of other objects. Therefore, when dealloc is its own, other objects with weak references need to be notified.