2012-01-05 2 views
1

Apple의 ARC를 사용하고 있습니다. 견인 클래스가 있습니다 : 하나는 NSMutableSet을 가지고 있고 하나는 NSMutableSet 안에 넣고 싶습니다.다른 객체 내부의 컨테이너에 객체를 추가하는 경우 호가 null 인 경우

그래서 다음 작업을 수행 할 때 :

ContainerClass* contClass = [[ContainerClass alloc] init]; 
    for(int i = 0;i<10;i++) 
    { 
     SmallClass* myClass = [[SmallClass alloc] init]; 
     [myClass setinfo:i]; 

     [contClass.mySet addObject:myclass]; 
    } 

    for(SmallClass* cls in contClass.mySet){ 
     NSLog(@"%@", cls); 
    } 

결과는 다음과 같습니다 (NULL) (NULL) (널) 등 (널)

이 SmallClass는 ARC에 의해 발표되고 있음을 의미합니까 ? 어떻게 해결할 수 있습니까 (나는 물론 할 수 없다)? myClass에 copy를 구현하지 않았기 때문에 myclass 인스턴스에서 copy를 호출하면 오류가 발생합니다 (이전 ARC 방식에서는 retain을 사용합니다).

ContainerClass 코드 :

@interface ContainerClass : NSObject { 
    NSString* _icon; 
    NSMutableSet* _mySet; 
} 

@property (nonatomic, strong) NSString* icon; 
@property (nonatomic, strong) NSMutableSet* mySet; 

@end 

및 구현 :

@implementation ContainerClass 

-(id) init 
{ 
    _myset = [[NSMutableSet alloc] init]; 
    return [super init]; 
} 

@synthesize mySet = _mySet; 

@end 
+0

ContainerClass에 대한 코드를 제공해 주시겠습니까? –

+0

'@interface ContainerClass : NSObject { NSString * _icon; NSMutableSet * _mySet; } @property (비 원자력, 강함) NSString * 아이콘; @property (비 원자력, 강함) NSMutableSet * mySet; end' @ 및하는 .m 파일 : '@implementation PoiSet - (ID)를 초기화하기 { _myset = [NSMutableSet의 ALLOC] INIT]; return [super init]; } @synthesize mySet = _mySet; @ end' – amir

+0

죄송합니다.이 코멘트 시스템에 익숙하지 않습니다. 여기에 더 나은 모양입니다 : http://codetidy.com/1725/ – amir

답변

1

나는 당신의 ContainerClass 초기화 방법이 잘못 생각합니다. 다음을 시도해보십시오.

-(id)init 
{ 
    if (self = [super init]) 
    { 
     _myset = [[NSMutableSet alloc] init]; 
    } 
    return self; 
} 

희망이 있습니다.

+0

감사합니다 또한 self.mySet = [[NSMutableSet alloc] init]; – amir