【发布时间】:2012-08-15 06:05:00
【问题描述】:
我正在尝试学习 Objective C。我遇到了编译器在后台为@property(nonatomic, retain) NSString* myField 生成的以下代码
-(NSString*) myField
{
return myField_; //assuming myField_ is the name of the field.
}
-(void) setMyField:(NSString*) newValue
{
if(newValue != myField_)
{
[myField_ release];
myField_ = [newValue retain];
}
}
现在我的问题是;为什么要在 newValue 上调用保留?相反,应该使用以下语法:
myField_ = newValue;
[myField_ retain];
请告知为什么不使用上述语法,因为根据我的理解,我们希望保留myField_ 指向的对象?
【问题讨论】:
标签: iphone objective-c