简体   繁体   English

Objective-C readonly属性是否需要指定strong或copy?

[英]Does an Objective-C readonly property need to specify strong or copy?

If I have a read-only string property, is it necessary to specify strong (or retain ) or copy in the declaration? 如果我有一个只读字符串属性,是否有必要在声明中指定strong (或retain )或copy 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. 在我看来,所有权属性仅在你有一个setter时才有用。

@property (nonatomic, readonly) NSString *name;

That is mostly correct. 这大多是正确的。 For a readonly property, strong , retain , weak , and assign have no effect. 对于readonly属性, strongretainweakassign不起作用。 But if you also declare the property elsewhere as readwrite (most frequently in an anonymous category in the .m ), then the other modifiers need to match. 但是,如果您还将其他属性声明为readwrite (最常见于.m中的匿名类别),则其他修饰符需要匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM