Objective-C 只读属性是否需要指定强或复制?
如果我有一个只读字符串属性,是否需要在声明中指定 strong
(或 retain
)或 copy
?如果我不指定,是否假定其中之一?
在我看来,所有权属性只有当你有 setter 时才有用。
@property (nonatomic, readonly) NSString *name;
If I have a read-only string property, is it necessary to specify strong
(or retain
) or copy
in the declaration? If I don't specify, is one of them assumed?
It seems to me the ownership attribute is only useful when you have a setter.
@property (nonatomic, readonly) NSString *name;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这基本上是正确的。对于
readonly
属性,strong
、retain
、weak
和assign
没有影响。但是,如果您还在其他地方将该属性声明为readwrite
(最常见的是.m
中的匿名类别),则其他修饰符需要匹配。That is mostly correct. For a
readonly
property,strong
,retain
,weak
, andassign
have no effect. But if you also declare the property elsewhere asreadwrite
(most frequently in an anonymous category in the.m
), then the other modifiers need to match.