转自:http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retai
问:
I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the both are the same in a garbage collected environment. I'm
mostly concerned with a non-GC based environment (eg: iPhone).
Directly from Scott's blog:
"The assign keyword will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates."
What does it mean that you don't directly own the delegate object? I typically retain my delegates, because if I don't want them to go away into the abyss, retain will take care of that for me. I usually abstract UITableViewController away from its respective
dataSource and delegate also. I also retain that particular object. I want to make sure it never goes away so my UITableView always has its delegate around.
Can someone further explain where/why I'm wrong, so I can understand this common paradigm in Objective-C 2.0 programming of using the assign property on delegates instead of retain?
Thanks!
答:
The reason that you avoid retaining delegates is that you need to avoid a retain loop (more commonly known as retain cycle):
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, 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.
分享到:
相关推荐
Objective-C is the universal language of iPhone, iPad, and Mac apps, and Objective-C for Absolute Beginners, Second Edition starts you on the path to mastering this language and its latest release. ...
The Dynamism of Objective-C 57 Extensions to the Objective-C Language 58 Using Objective-C 64 The Root Class 66 NSObject 67 Root Class—and Protocol 67 Overview of Root-Class Methods 68 Interface ...
dr-charts用于Objective-C中iOS的易于使用,可定制和交互式的图表功能:多种图表类型线/多线/与X和Y轴平行的线-圆形图表...交互性支持-可以使用Delegates高度可定制的Objective-C,iOS 8+演示安装CocoaPods轻松访问
Delegates-and-Events-in-CSharp
Finally, in the last chapter we leave the confines of ANSI-C and implement the obligatory mouse-operated calculator, first for curses and then for the X Window System. This example neatly demonstrates...
Apple developed Swift to address the limitations of Objective-C, and add features found in more complex languages like Python. The results is simpler, cleaner, more expressive code with automatic ...
A Learner’s Guide to Creating Objective-C Applications for the iPhone and iPad Book Description : Let’s say you have a killer app idea for iPhone and iPad. Where do you begin? Head First iPhone and...
C#中的委托与事件 delegates-and-events 源码例子
Addison Wesley, 2009 ...Cocoa Design Patterns clearly defines the problems each pattern solves with a foundation in Objective-C and the Cocoa frameworks and can be used by any Mac or iPhone developer.
包含一个kotlin属性委托的集合,用于管理android意图和捆绑附加组件。
Take advantage of iPhone OS patterns such as datasources and delegates Preview your applications in the Simulator Build more complicated interactions that utilize multiple views, data entry/editing...
Learn-Events-Delegates-And-Lambdas 来自 PluralSight C# 事件、委托和 Lambda 模块 1 - 事件、委托和事件处理程序 事件 - 发送给订阅者的通知 (Button_Click) 鼠标按下触发点击事件。 多个对象可以通过订阅事件...
Delegates-and-Events-in-CSharp
The results of that API are passed into some type of parser (a custom parser or one that uses reflection or whatever) to translate it into data objects. From there the results are returned to the ...
which delegates to the given context Source Code for Andriod.
signature Schemes Based on the Elliptic Curve Cryptosystem,薛庆水,李凤英,In a proxy signature scheme, one original signer delegates a proxy signer to sign messages on behalf of the original signer....
_.pluck(list, propertyName) A convenient version of what is perhaps the most common use-case for map: extracting a list of property values. _.max(list, [iterator], [context]) Returns the maximum value...
Compare Swift with Objective-C, JavaScript, Python, Ruby, and C Collect data with arrays and dictionaries, and store it with variables and constants Group commonly-used code into functions for easy ...
The book helps readers avoid hidden pitfalls of C# programming by understanding "behind the scenes" issues. 目录: Part 1 Preparing for the journey Chapter 1 The changing face of C# ...