[英]Using @property on ARC
// .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;
之间有什么区别_note = note;
和self.note = note;
然后? 而不是strong
, retain
非ARC会在这种情况下产生影响。
如果我正确理解了这个问题......
如果要覆盖setter,则需要指定_propertyName
而不是self.propertyName
,以避免无限递归:
- (void)setNote:(NSString *)note
{
_note = note;
// self.note = note; // <-- doing this instead would freeze, and possibly crash your app
}
如果你压倒吸气器也一样。 在其他情况下,您可以使用两者中的任何一个。
如果您使用(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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.