__strong of Arc mechanism
__strong parsing :
By default, a pointer uses the __strong property to indicate that this is a strong reference. This means that the object cannot be destroyed as long as the reference exists. This is the desired behavior: when all (strong) references are removed, the object can be collected and freed.
However, there are times when we want to disable this behavior: Some collection classes should not increase the references to their elements, because this causes the object to be unable to be freed. In this case, we need to use a weak reference (don't worry, the built- in collection class is doing this), using the __weak keyword. nshashtable is an example. When the referenced object disappears, the weak reference is automatically set to nil. Cocoa 's Notification Center is such an example, though it is beyond the pure objective-c language category
Own deeper understanding:
1. (Weak and strong) is different: When an object no longer has a strong type pointer to it, it will be released, even if the object has a _weak type pointer to it;
Strong in OC is equivalent to the Retain attribute, and weak is equivalent to assign. There is only one situation where you need to use weak (the default is strong), just to avoid retain cycles (that is, the parent class has a subclass {parent class retain the subclass}, and the child class calls the parent class {subclass and retain the parent class}, which cannot be release)
2. once the last pointer to the strong type of the object is left, the object will be freed, and if there is a weak pointer pointing to the object, all remaining weak pointers will be cleared out.
Here are some of the questions I encountered while reading
(1): __strong is available when arc is off
(2): __strong modified variable, directly assigned value, the value of the retain of the assigned variable will be self-increment (the assigned variable retain will be self-reducing)
(3): __strong modified variables are retain when they go beyond their scope
The answers are as follows:
(1): Can
(2): for example:
int count = 1;
ID __strong obj1 =nil;
ID __strong obj2 = [[nsobjectalloc] init];
ID __strong obj = [[nsobjectalloc] init];
obj1 = obj; Obj1 Holding a strong reference to an obj assignment object causes the obj retain to increment
NSLog(@ "Retain count is%ld",cfgetretaincount(__bridge cftyperef));
obj = obj2;
NSLog(@ "obj =%ld",cfgetretaincount(__bridge Cftyperef));
Print as follows :
2015-07-22 17:23:22.169 Dictionary [724:25567] Retain count is 2
2015-07-22 17:23:22.171 Dictionary [724:25567] obj = 2
Description: (1): When an object obtains a strong reference to another object,retain++;
(2): The assigned variable retain is self-reducing
(3):
int count =1;
ID __strong obj1 =nil;
if (count) {
ID __strong obj = [[nsobjectalloc] init];
obj1 = obj; Obj1 Holding a strong reference to an obj assignment object causes the obj retain to increment
NSLog(@ "Retain count is%ld",cfgetretaincount(__bri Dgecftyperef));
}
NSLog(@ "Retain count is%ld",cfgetretaincount(__bridge cftyperef) (obj1));
__strong modified variables are retain when they go beyond their scope.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
__strong of Arc mechanism