Leonardo
Leonardo

Reputation: 9857

Getter and setter and property retain in objective-c

I have a class wich is initialized like this.

// myclass.h
@property(nonatomic,retain) NSMutableArray *daysOfWeek; // this is in .h file

// myclass.m
@synthesize daysOfWeek;

-(id)init {
            if(self=[super init]) {
                    // initialize days of week
                    self.daysOfWeek = [NSMutableArray arrayWithCapacity:0];
            }
            return self;
    }

however later during application lifecycle, seems that daysOfWeek get freed. If I add retain in init method:

self.daysOfWeek = [[NSMutableArray arrayWithCapacity:0] retain];

then everything works as expected, and I can add and retrieve object from daysOfWeek. I tought that synthesize would retain the daysOfWeek, what am I missing here ?

thanks

Upvotes: 0

Views: 277

Answers (1)

swegi
swegi

Reputation: 4112

The problem lies somewhere else. Your original init is fine.

Upvotes: 3

Related Questions