0

I am a bit confused by the underscore naming and the self. Is the following code OK?

@interface MyClass()

@property (nonatomic,retain) NSString        *name;
@property (nonatomic,retain) NSString        *surname;

@end

@implementation MyClass
@synthesize name = _name;
@synthesize surname = _surname;

Then when I need to assign a value I do this

self.name = [someParam retain];
4

3 回答 3

2

不,这是不行的,因为它self.name还将保留您在属性中声明的值。

这将保留:

self.name = someParam;

其他方式写入相同(注意释放当前变量):

if (_name != someParam) {
    if(_name) {
       [_name release], _name = nil;
    }
    _name = [someParam retain];
}
于 2012-05-11T09:55:30.267 回答
1

你不必重复保留:)

只是:

self.name = someParam;

但其余的我认为还可以

于 2012-05-11T09:56:09.250 回答
0

不,这是不对的,因为您必须使用别名 _name

if(_name)
{
    [_name release];
    _name=nil;
}
else
{
    //retain the object
}
于 2012-05-11T10:12:12.453 回答