_ Weak, _ strong, and autorelease usage

Source: Internet
Author: User

When the block version of the container is used as the enumerator, an autoreleasepool is automatically added:

The autorelease object is released at the end of the current runloop iteration, because the system adds the Auto Release pool push and pop in each runloop iteration.

Of course, there is no common for loop or for in loop, so it is more convenient to use the block version enumerator of the new version. When traversing in a for loop produces a large number of autorelease variables, You need to manually add a local autoreleasepool.

[Array enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {// surrounded by a local @ autoreleasepool}];

 

 

Weak and strong are a mechanism for iOS to release objects. The objects declared by weak will be released to nil after the original pointer is released;

The following code and output of _ weak and _ strong are provided.

@ Implementation autoreleaseviewcontroller _ weak ID reference;-(void) viewdidload {[Super viewdidload]; @ autoreleasepool {nsstring * STR = [nsstring stringwithformat: @ "XXXXX"]; reference = STR; nslog (@ "str ADDR is % P", STR); nslog (@ "reference ADDR is % P", reference) ;}}-(void) viewwillappear :( bool) animated {[Super viewwillappear: animated]; nslog (@ "reference ADDR is % P", reference); nslog (@ "view will appear reference: % @", reference);}-(void) viewdidappear :( bool) animated {[Super viewdidappear: animated]; nslog (@ "viewdidappear reference: % @", reference );} @ end // output 19:40:34. 480 autorelease [4506: 60b] str addr is 0x8f328902014-11-01 19:40:34. 480 autorelease [4506: 60b] reference ADDR is 0x8f328902014-11-01 19:40:34. 480 autorelease [4506: 60b] reference ADDR is 0x02014-11-01 19:40:34. 481 autorelease [4506: 60b] view will appear reference :( null) 19:40:34. 486 autorelease [4506: 60b] viewdidappear reference: (null)
@ Implementation autoreleaseviewcontroller _ strong ID reference;-(void) viewdidload {[Super viewdidload]; @ autoreleasepool {nsstring * STR = [nsstring stringwithformat: @ "XXXXX"]; reference = STR; nslog (@ "str ADDR is % P", STR); nslog (@ "reference ADDR is % P", reference) ;}}-(void) viewwillappear :( bool) animated {[Super viewwillappear: animated]; nslog (@ "reference ADDR is % P", reference); nslog (@ "view will appear reference: % @", reference);}-(void) viewdidappear :( bool) animated {[Super viewdidappear: animated]; nslog (@ "viewdidappear reference: % @", reference );} @ end // output 19:41:20. 872 autorelease [4669: 60b] str addr is 0x8d42dd02014-11-01 19:41:20. 872 autorelease [4669: 60b] reference ADDR is 0x8d42dd02014-11-01 19:41:20. 873 autorelease [4669: 60b] reference ADDR is 0x8d42dd02014-11-01 19:41:20. 873 autorelease [4669: 60b] view will appear reference: xxxxx2014-11-01 19:41:20. 877 autorelease [4669: 60b] viewdidappear reference: XXXXX

 

_ Weak, _ strong, and autorelease usage

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.