1

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
amir
  • 1,332
  • 8
  • 15
  • Could you provide the code for your ContainerClass? – Lorenzo B Jan 05 '12 at 10:32
  • `@interface ContainerClass : NSObject { NSString* _icon; NSMutableSet* _mySet; } @property (nonatomic, strong) NSString* icon; @property (nonatomic, strong) NSMutableSet* mySet; @end` and the .m file: `@implementation PoiSet -(id) init { _myset = [[NSMutableSet alloc] init]; return [super init]; } @synthesize mySet = _mySet; @end` – amir Jan 05 '12 at 10:38
  • sorry i am new to this comments system. here is a better look: http://codetidy.com/1725/ – amir Jan 05 '12 at 10:40
  • You can just edit your post and add your new code. For example under your **note** section, you could add an **edit** section and put the code there. – Lorenzo B Jan 05 '12 at 10:45
  • thanks i edited it, also thanks for the corrections, i will try to improve next time. – amir Jan 05 '12 at 10:50

1 Answers1

1

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190