0
// .h
@property ( strong, nonatomic ) NSString *note;

// .m
@synthesize note = _note;

- ( id ) initWithNote: ( NSString * )note {

    self = [ super init ];
    if ( self ) {
        _note = note;   // _note is just a instance variable.
        self.note = note;   // 'self.note = note;' is using setter method.
        return self;
    }
    return nil;
}

@property ( strong, nonatomic ) NSString *note;影响 setter 和 getter 方法。默认情况下,变量在 ARC 上是 __strong 类型。

_note = note;self.note = note;then有什么区别?而不是strong,retain在非 ARC 上在这种情况下有所作为。

4

2 回答 2

1

如果我正确理解了这个问题...

如果要覆盖 setter,则要分配 to_propertyName而不是self.propertyName,以避免无限递归:

- (void)setNote:(NSString *)note
{
    _note = note;
    // self.note = note; // <-- doing this instead would freeze, and possibly crash your app
}

如果你覆盖吸气剂,同样的事情。在其他情况下,您可以使用两者中的任何一个。

于 2013-02-23T08:12:58.970 回答
1

如果你使用它们,它们现在实际上是相同的(nonatomic)(atomic)但是,如果您使用(默认设置)或更可能定义自定义设置器,它们会有所不同:

- (void)setNote:(NSString *)note {
    // Do something fancier than this
    _note = note;
}
self.note = note; // use the custom setter

相对

_note = note; // set the variable directly
于 2013-02-23T08:13:46.693 回答