对象、变量、变量修饰符、赋值
1、站在对象和引用计数的角度看:我不关心谁拥有我,我只关心谁想我发出了维护消息;
[_dog release];
2、任何变量的赋值,都代表了内存规则的进一步维护;
引用计数的语义是什么?
指针,内存变量、对象
strong、retain、release:向对象发送消息,修改引用计数的值;
赋值操作:使用变量修饰符对内存维护机制发送维护信息;
Variable Qualifiers
You use the following lifetime qualifiers for variables just like you would, say, const.
- __strong is the default. An object remains “alive” as long as there is a strong pointer to it.
- __weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
- __unsafe_unretained specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.
- __autoreleasing is used to denote arguments that are passed by reference (id *) and are autoreleased on return.
https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html
You don't have to explicitly specify __autoreleasing when defining a function that returns an object, for example
-(BOOL)doSomething:(NSError **)error;
The ARC compiler automatically inserts the __autoreleasing. This is explained in the Clang/ARC documentation:
4.4.2 Indirect parameters
If a function or method parameter has type T*, where T is an ownership-unqualified retainable object pointer type, then:
- if T is const-qualified or Class, then it is implicitly qualified with __unsafe_unretained;
- otherwise, it is implicitly qualified with __autoreleasing.
The Xcode code completion also knows about that and displays (NSError *__autoreleasing *)error.
When calling such a function the ARC compiler also automatically does "the right thing", so you can just call
NSError *error;
BOOL success = [self doSomething:&error];
As explained in the "Transitioning to ARC Release Notes", the compiler inserts a temporary __autoreleasing variable:
NSError *error;
NSError * __autoreleasing tmp = error;
BOOL success = [self doSomething:&tmp];
error = tmp;
(For the gory details you can read 4.3.4 "Passing to an out parameter by writeback" in the Clang/ARC documentation.)
https://stackoverflow.com/questions/23268576/autoreleasing-in-errornserror-autoreleasing-outerror