这段代码非常简单,但是是否正确呢?我不确定是否需要保留通过init方法传递的委托。
@interface SomeClass : NSObject {
SomeClassDelegate *someClassDelegate;
}
-(id)initWithDelegate:(SomeClassDelegate *)delegate;
@end
@implementation SomeClass
-(id)initWithDelegate:(SomeClassDelegate *)delegate
{
[delegate retain]; // should I be doing this?
someClassDelegate = delegate;
}
-(void)dealloc
{
[delegate release]; // obviously only do this if I DO need to retain it
[super dealloc];
}
@end
我的初步想法是否定的,然而这段代码似乎暗示了另一种情况。我知道我不能依赖retain counts,但我想知道处理委托的正确方法。
// self's retain count is 1
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
// the retain count is now 2, did the init of URLConnection retain self?