这段代码非常简单,是否正确?我不知道是否应该保留通过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
Run Code Online (Sandbox Code Playgroud)
我最初的想法是否定的,但是这段代码似乎暗示了其他方面.我知道我不能依赖保留计数,但我想知道处理代表的正确方法.
// 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?
Run Code Online (Sandbox Code Playgroud)