【发布时间】:2012-07-12 08:36:24
【问题描述】:
我定义了以下变量:
@property (nonatomic, retain) NSMutableArray *arraySpeechSentences;
我正在尝试通过以下方式对其进行初始化:
// Set the array of sentences to the stored array
NSMutableArray *speechSentences = [[NSMutableArray alloc] initWithArray:[tempDict objectForKey:key]];
arraySpeechSentences = speechSentences;
[speechSentences release];
当我尝试调用 [arraySpeechSentences count] 时,应用程序崩溃。但是,如果我按以下方式设置变量:
// Set the array of sentences to the stored array
NSMutableArray *speechSentences = [[NSMutableArray alloc] initWithArray:[tempDict objectForKey:key]];
self.arraySpeechSentences = speechSentences;
[speechSentences release];
我可以很好地调用 [arraySpeechSentences count]。我的印象是,如果你使用 self.它只是检查变量是否已经设置,如果是,它将在分配新值之前释放对象。我是不是弄错了,如果是这样,我什么时候应该使用 self.设置值?
感谢您的帮助, 艾略特
【问题讨论】:
-
在处理属性时始终使用 self.propertyName。并在 dealloc 中以相同的方式将它们归零。
-
你的问题的第一行可能已经出现了混淆:当你使用
@property时,你没有声明一个变量,而是一个属性。因此需要self,实例本身内的所有属性访问都需要它。 -
@TeodorCarstea 除非您覆盖 setXXX 方法,在这种情况下您应该访问底层实例变量(并自己处理保留)
-
请参阅我的answer 到上一个问题,这可能有助于您理解
@property和@synthesize
标签: iphone objective-c ios