Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Delegate: to retain or not?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Delegate: to retain or not?



On Monday, July 15, 2002, at 03:51 , petite_abeille wrote:

A awful doubt crossed my tired mind: should I retain my delegates or not?

Nope. The convention is not to retain them, on the presumption that the delegate is generally a controller.

If I do, am I not creating "a clear and present danger" of retain cycles?

Exactly. Some very specific situations apart, the controller (which is also the delegate) should retain its views and other helper objects (of whose it is the delegate).

If I don't, should I use something along the line of [NSValue valueWithNonretainedObject]? Or simply assign the delegate to my ivar? Can I use NSIsFreedObject to check if the object is still around?

See above. The general pattern should be (conceptually, the actual code of course tends to differ):

@implementation SomeController ...
-init... {
[foo retain];
[foo setDelegate:self];
[bar retain];
[bar setDelegate:self];
}
-(void)dealloc {
[foo setDelegate:nil];
[foo release];
[bar setDelegate:nil];
[bar release];
}
@end

Am I hopelessly confused?!?

Not really. The delegates are somewhat confusing, since there are good and sound reasons for both approaches (to retain / not to retain), but the convention I've just outlined was selected in NeXT ages ago as, so to speak, the lesser evil. And, in my experience, it is quite good indeed: I'
ve got my share of retain/release problems of course, but never with delegation.
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >Delegate: to retain or not? (From: petite_abeille <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.