Why does delegate use assign when declaring attributes?

Source: Internet
Author: User

Today, a colleague asked me why I used assign to declare attributes in objective-C delegate. I checked the information on the Internet and summarized it as a memory management problem in objective-C. I copied the foreigner's answer directly:

 

The reason that you avoid retaining delegates is that you need to avoid a retain loop:

A creates B a sets itself as B's delegate... A is released by its owner

If B had retained a, a wouldn't be released, as B owns a, thus a's dealloc wocould never get called, causingBoth A and BTo leak.

You shouldn't worry about a going away B/C it owns B and thus gets rid of it in dealloc.

Link: http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retain

 

You can understand it. Stackoverflow is really easy to use and solves many of my technical problems.

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.