我正在尝试学习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_
?