IOS __weak Learning Questions

Source: Internet
Author: User


__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

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.