문제

I am using Apple's ARC. I have tow classes: one that has a NSMutableSet and one that I want to put inside that NSMutableSet.

So when I do the following:

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

the result is: (null) (null) (null) (null) etc.

Does it means that SmallClass is being released by ARC? How can I solve it (I can't do retain of course)? Calling copy on myclass instance results with error because I didn't implement copy for myClass (in the pre-ARC way I would just do retain on it).

ContainerClass code:

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

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

@end

and the implementation:

@implementation ContainerClass

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

@synthesize mySet = _mySet;

@end
도움이 되었습니까?

해결책

I think your ContainerClass init method is wrong. Try the following:

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

Hope it helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top