为什么delegate在声明属性的时候要使用assign?

今天一位同事问我为什么在Objective-C中delegate声明属性的时候要使用assign。在网上查了一下资料,归结为Objective-C中的内存管理问题,直接把老外的answer copy了过来:

 

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 would never get called, causing both A and B to leak.

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

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

 

应该看得懂吧。stackoverflow的确很好用啊,解决了我不少的技术问题。

你可能感兴趣的:(delegates)