2013-12-13 1 views
0

Objective-C의 새로운 기능으로 참조 카운트가 혼란 스럽습니다 .--(. Xcode 5.0.2의 ARC 모드에서 NSArray init 객체를 만들면 객체의 dealloc 메톤이 호출되지 않고, ? 왜 여기 내 테스트 코드는 어떻게 수동으로 배열에서 개체를 제거하지만이있는 NSArray, 야 하는가?.NSArray의 객체가 ARC 모드에서 dealloc methon을 호출하지 않는 이유는 무엇입니까?

//------LCDRound.h file------------- 
@interface LCDRound : NSObject 
- (void)paint; 
@end 
//------LCDRound.m------------------ 
@implementation LCDRound 
- (void)paint 
{ 
    NSLog(@"I am Round"); 
} 
- (void)dealloc 
{ 
    NSLog(@"Round dealloc"); 
} 
@end 

//-------main.m--------------------- 
#import <Foundation/Foundation.h> 
#import "LCDRound.h" 
int main(int argc, const char * argv[]) 
{ 
    LCDRound* round1 = [[LCDRound alloc] init]; 
    LCDRound* round2 = [[LCDRound alloc] init]; 
    NSArray* objects = [NSArray arrayWithObjects:round1, round2, nil]; 
    for (LCDRound* shape in objects) { 
     [shape paint]; 
    } 
    return 0; 
} 
+1

예제에서 dealloc을 호출하는 위치는 어디입니까? – DrummerB

답변

4

[NSArray arrayWithObjects:…]는 오토 릴리즈 객체, 를 반환하고 프로그램이 오토 릴리즈 풀을 제공하지 않습니다 (이는 이전 버전의 iOS/OS X에서 런타임에 경고를 발생시키는 데 사용되었습니다.

NSArray* objects = [[NSArray alloc] initWithObjects:round1, round2, nil]; 

또는 오토 릴리즈 풀을 추가

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
     LCDRound* round1 = [[LCDRound alloc] init]; 
     LCDRound* round2 = [[LCDRound alloc] init]; 
     NSArray* objects = [NSArray arrayWithObjects:round1, round2, nil]; 
     for (LCDRound* shape in objects) { 
      [shape paint]; 
     } 
    } 
    return 0; 
} 

은 당신이 당신의 dealloc을 다시 볼 수 있습니다.

+0

작동합니다! 감사합니다! :) – Qing