2016-12-13 6 views
-1

ARC가있는 프로젝트와없는 프로젝트의 차이점을 연구 중입니다. 프로토콜을 구현해야하는 사용자 정의 라이브러리를 사용합니다. ARC가없는 프로젝트에서 콜백 (onEvent)이 호출되었지만 ARC 프로젝트에서는 발생하지 않습니다. 가() 콜백의 onEvent를 불렀다 - - 내가 프로토콜 코드에 액세스 할 필요가 없습니다ARC가 작동하지 않는 프로토콜

을하지만 난 다음 구현 틀렸다 생각 :

의 ViewController :

@implementation ViewController 

- (IBAction)pressed:(id)sender { 
    ObjectA *sd = [[ObjectA alloc] init]; 

    [sd startWith:@"myString"]; 
} 

인터페이스 ObjectA :

@interface ObjectA : NSObject <MyProtocol> 

@property (nonatomic, strong) id<MyProtocol> myProtocol; 
- (void) startWith:(NSString *)myString; 
@end 

구현 ObjectA :

- (void) startWith:(NSString *)myString 
{ 

    id<MyProtocol> protocolDelegate = self; 
    self.myProtocol = [[ObjectB alloc] init: protocolDelegate]; 
    [self.myProtocol doStuff]; 
} 

- (void) onEvent 
{ 
    NSLog(@"Called"); 
} 

인터페이스 ObjectB :

@interface ObjectB : NSObject<MyProtocol> 

질문

은 다음과 같습니다 왜 ARC 프로젝트 "의 onEvent은"호출되지 않습니다?

의견을 보내 주셔서 감사합니다.

편집 :이 방법으로의 ViewController을 변경 해결 한

:

귀하의 설명과 솔루션을 기반으로
@interface ViewController() 
    @property (nonatomic, strong) ObjectA *sd; 
    @end 

     @implementation ViewController 
    - (void)viewDidLoad { 
     _sd = [[ObjectA alloc] init]; 
    } 
    - (IBAction)pressed:(id)sender {  
     [sd startWith:@"myString"]; 
    } 
+1

'onEvent'에 대한 호출이 표시되지 않습니다. – shallowThought

+0

내가 말했듯이, 나는 프로토콜을 코딩 할 수있는 권한이 없다. 이 프로토콜은 라이브러리에서 사용되기 때문입니다. –

+0

이해하십시오. '@property (nonatomic, strong) TypeOfClassFromLibrary myLibClassInstance; '속성을 만들어야합니다. 그런 다음'viewDidLoad' :'self.myLibClassInstance = [[TypeOfClassFromLibrary alloc] init]; self.myLibClassInstance.nameOfProtocol = self;' – shallowThought

답변

0

, 내가 설명을 가지고있다.

메모리 누수로 인해 수동 참조 카운팅 (비 ARC) 버전이 작동했습니다.

- (IBAction)pressed:(id)sender { 
    ObjectA *sd = [[ObjectA alloc] init]; 

    [sd startWith:@"myString"]; 
} 

ObjectA의 인스턴스를 불렀다 누를 때마다

는 할당 된, 그러나 당신이 볼 수 있듯이, 저장 아니에요, 반환되지 않습니다, 그리고 출시 아닙니다. 그래서 인스턴스가 방금 누출되었습니다.

-onEvent 콜백에 대해 ObjectA의 누출 된 인스턴스가 여전히 존재하는 것이 좋습니다. 아래쪽은 당신이 기억을 새고 있었다는 것이 었습니다.

+0

예, 문제입니다. 실제로 "분석"도구로 메모리 누수가 발생했기 때문에 문제가 해결되었습니다. 당신의 설명에 감사드립니다. –