__weak Weak references do not hold objects, so variables assigned to the __weak modifier do not change the value of the counter.
Main.m
ID __strong obj3 =nil;
ID __weak obj1=nil;
/*
ID __weak obj1 = obj;
The compiler's impersonation code is as follows :
ID obj1;
Objc_iniitweak (&obj1,obj);
Objc_destroyweak (&OBJ1);
*/
@autoreleasepool {
ID obj = [[nsobjectalloc] init];
Obj1 = obj;
Obj3 = obj;
nslog (@ "%d" , _objc_ Autoreleasepoolprint ()); // arc view pool What's inside the pool
nslog ( @" obj Retaincount =%ld " (( __bridge cftyperef
nslog ( @" obj1 Retaincount =%ld ", Cfgetretaincount (( __bridge cftyperef
NSLog(@ "obj3 Retaincount =%ld",cfgetretaincount((__bri Dgecftyperef) (obj3 ));
}
nslog ( @ "Obj3 Retaincount =%ld" cfgetretaincount (( __bridge cftyperef
NSLog(@ "obj1 Retaincount =%ld",cfgetretaincount((__ Bridgecftyperef)(obj1 ));
NSLog (@ "%d", _objc_autoreleasepoolprint ());
/*
Print as follows:
OBJC[1029]: ##############
OBJC[1029]: autorelease pools for thread 0x7fff7455a300
OBJC[1029]: 1 releases pending.
OBJC[1029]: [0x101001000] ......... PAGE (Hot) (cold)
OBJC[1029]: [0x101001038] ################ POOL 0x101001038
OBJC[1029]: ##############
2015-07-24 23:02:39.686 objective-c Objects and Core Foundation Object [1029:100223] 139077936
< strong>2015-07-24 23:02:39.687 objective-c object with core Foundation object [1029:100223] obj retaincount = 2 -------->>>>> >> (1)
< strong>2015-07-24 23:02:39.688 objective-c object with core Foundation object [1029:100223] obj1 retaincount = 3 -------->>>>>>> (2)
< strong>2015-07-24 23:02:39.688 objective-c object with core Foundation object [1029:100223] obj3 retaincount = 2 -------->>>>>>> (1)
< strong>2015-07-24 23:02:39.688 objective-c object with core Foundation object [1029:100223] obj3 retaincount = 1 -------->>>>>>> (3)
2015-07-24 23:02:39.688 objective-c Objects and Core Foundation Object [1029:100223] obj1 retaincount = 2-------->>>>>>> (4)
OBJC[1029]: ##############
OBJC[1029]: autorelease pools for thread 0x7fff7455a300
OBJC[1029]: 0 releases pending.
OBJC[1029]: [0x101001000] ......... PAGE (Hot) (cold)
OBJC[1029]: ##############
2015-07-24 23:02:39.688 objective-c Objects and Core Foundation Object [1029:100223] 139077936
*/
/*
1: The variable modified by the above (1) __weak modifier does not hold the object
2: By (3) know that when the __strong modified variable is outside the scope, not holding the object, resulting in rerainCout-1;
3: The attentive reader may have discovered that Obj1 's retaincount is not the same as Obj's? (This is also the author's biggest doubts)
The following describes the author's thinking process
(1) The __weak modified variable, although a weak reference to the object, does not change the reference count (Retaincount) of the Assignment object (obj), but its own retaincount increases. -------->>> But they're pointing at the same address, why retaincount different? So this way doesn't work.
(2) Although they are the same address, but __weak do not hold objects, but __weak modified variables want to use the object must be Retaincount + 1, but at the same time does not change the object's retaincount. With this question I finally found the answer-->> When looking for an object through the __weak pointer, it has a modifier that will make the returned Retaincount + 1 when using him (note that this is not the Retaincount itself).
4: Holder does not hold an object, is to see if it causes the object's Retaincount + 1, rather than whether he points to that address.
*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS __weak Learning Questions