adding objects in a container inside other object is null with arc

120 views Asked by At

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
1

There are 1 answers

0
Lorenzo B On BEST ANSWER

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.