[英]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. 但是,在应用程序生命周期的后期,似乎可以释放daysOfWeek。 If I add retain in init method: 如果我添加保留init方法:
self.daysOfWeek = [[NSMutableArray arrayWithCapacity:0] retain];
then everything works as expected, and I can add and retrieve object from daysOfWeek. 然后一切都会按预期工作,我可以从daysOfWeek添加和检索对象。 I tought that synthesize would retain the daysOfWeek, what am I missing here ? 我坚信合成将保留daysOfWeek,我在这里缺少什么?
thanks 谢谢
The problem lies somewhere else. 问题出在其他地方。 Your original init
is fine. 您最初的init
很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.