简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM