NSString属性复制还是只读? [英] NSString property copy or readonly?

查看:71
本文介绍了NSString属性复制还是只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多讨论都说我应该对NSString属性使用 copy ,因为它会阻止其他人在我的背后进行更改.但是,为什么不为它设置 readonly 属性呢?

I see many discussions saying that I should use copy for NSString property because it will prevent others from changing it behind my back. But then why don't we just set readonly property for it?

更新

感谢您回答我的问题.但事实是,对于NSString属性,您始终不希望其他人对其进行修改,对吗?您可以自己修改它,但绝对不能修改.我猜大多数时候NSString都会设置它的初始值(由您或其他人设置),之后只有您才能对其进行修改.那为什么不只使用readonly属性

Thanks for answering my question. But the thing is that for NSString property, you always don't want others to modify it, right? You may modify it yourself but definitely not others. I guess most of time NSString get its initial value set up (either by you or by others), after that only you will modify it. Then why not just use readonly property

实际上,我大部分时间都在使用复印件.但是后来我意识到大部分时间我只在自己的init方法中使用这些设置方法.因此,我认为在这种情况下,我应该使用只读而不是复制.

Actually I use copy most of time. But then I realize most of time I only use those setters in my init method. So I think I should use readonly instead of copy for those case.

因此,我以这种方式提出问题:如果您仅在init方法中为NSString使用这些设置器,则应改用readonly.这是一个合理的结论吗?

So let me ask question in this way: if you only use those setters for your NSStrings in your init method, then you should use readonly instead. Is this a reasonable conclusion ?

推荐答案

如果您仅在init方法中为NSString使用这些设置器,则应改用readonly.这是一个合理的结论吗?

if you only use those setters for your NSStrings in your init method, then you should use readonly instead. Is this a reasonable conclusion?

由于不应在部分构造的状态(init/dealloc)中使用访问器,因此应将其声明为copyreadonly,然后在初始化程序中执行复制:

Since you should not use accessors in partially constructed states (init/dealloc), then you should declare it as copy and readonly, then perform the copy in the initializer:

- (id)initWithName:(NSString *)inName
{
  self = [super init];
  if (0 != self) {
    name = [inName copy];
  }
  return self;
}

更详细地讲,copyreadonly在语义上是不同的概念.

In more detail, copy and readonly are semantically different concepts.

  • 之所以使用copy,是因为在大多数情况下您对值感兴趣.使用不可变字符串也是一种保障和优化.

  • You use copy because you are interested in the value in most cases. It's also a safeguard and an optimization to use immutable strings.

您使用readonly禁止客户端更改/设置您的数据.

You use readonly to prohibit clients from mutating/setting your data.

它们一起提供了很好的安全性,但单独使用:

Together, they provide a good degree of safety, but alone:

  • copy仍然允许客户端通过设置器在程序执行的任何时候设置值.

  • copy still allows clients to set the value at any point in the program's execution via the setter.

readonly并不表示copy,保留的属性可能会在您的背后改变.考虑一下当您传递一个可变变体并且客户端在调用setter之后对其进行变异时会发生什么情况.

readonly does not imply copy, and a retained property could be changed behind your back; consider what happens when you are passed a mutable variant and the client mutates it after calling the setter.

最安全的方法是使用copyreadonly.

The safest way is to use copy and readonly.

  • 很明显,当您需要为客户提供设置器并支持该更改时,将使用readwrite.

保留字符串(或数组或...)而不是复制通常是一个坏主意.对于您来说,不复制这些类型几乎没有什么用,这可能会导致细微的错误.即使在处理可变类型时,通常也需要可变副本(编译器不会为您合成).保留或分配这些类型几乎从来都不是您想要的.我遇到的一个例外是在处理大型分配时,数据被很好地封装(例如,沉重的NSMutableData实例,我将所有权从一个地方传递到另一个地方以避免复制).

retaining a string (or array, or...) instead of copying is usually a bad idea. there is rarely a good use for you not to copy these types, and it can lead to subtle errors. even when you are dealing with a mutable type, you'll usually want a mutable copy (which the compiler will not synthesize for you). retaining or assigning these types is almost never what you want. one exception i make is when dealing with large allocations, where the data is encapsulated well (e.g. a heavy NSMutableData instance which I pass ownership from one place to another to avoid a copy).

这篇关于NSString属性复制还是只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆