2012-01-05 36 views
1

我正在使用Apple的ARC。我有兩個類:一個擁有一個NSMutableSet,另一個我想放入該NSMutableSet。在其他對象內的容器中添加對象爲空爲圓形

所以,當我做到以下幾點:

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); 
    } 

結果是:(空)(空)(空)(空)等

是否意味着小類是由ARC發佈?我該如何解決它(我當然不能保留)?在myclass實例上調用複製導致錯誤,因爲我沒有實現myClass的複製(在ARC之前我會保留它)。

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的代碼嗎? – 2012-01-05 10:32:04

+0

'@interface ContainerClass:NSObject {0} {0} {NSString * _icon; NSMutableSet * _mySet; } @property(nonatomic,strong)NSString * icon; @property(nonatomic,strong)NSMutableSet * mySet; @ end' 和.m文件: '@implementation PoiSet - (ID)初始化 { _myset = [[ALLOC的NSMutableSet] INIT]; return [super init]; } @synthesize mySet = _mySet; @ end' – amir 2012-01-05 10:38:33

+0

對不起,我是這個評論系統的新手。這裏是一個更好的外觀: http://codetidy.com/1725/ – amir 2012-01-05 10:40:45

回答

1

我覺得你ContainerClass init方法是錯誤的。請嘗試以下操作:

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

希望它有幫助。

+0

謝謝我也做了self.mySet = [[NSMutableSet alloc] init]; – amir 2012-01-05 11:11:55