이 코드는 매우 간단합니다. 맞습니까? 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
초기 생각은 아니지만,이 코드는 그렇지 않은 것으로 보입니다. 보유 횟수에 의존 할 수는 없지만 대리인을 처리하는 적절한 방법을 알고 싶습니다.
// 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?
NSURLConnection은 특별한 경우입니다. 델리게이트 메시지 수신을 막을 수있는 유일한 방법은 연결을 취소하는 것이므로 대리자를 유지하는 것이 아니라 연결 기간 동안 유지합니다. 즉, 대개 대리자를 보유하지 않으며 NSURLConnection의 동작은 구현 세부 사항입니다. 당신이 의존하거나 일반적으로 모방해야하는 것이 아닙니다. 또한 _class_ 대리인이있는 경우는 드뭅니다. 일반적으로 대리인 프로토콜을 선언합니다. –
감사합니다. 마이크, 그게 내가보고있는 것을 설명해줍니다. – Jab