“复制”和“复制”有什么区别?和“保留”?

发布于 2024-08-24 09:39:54 字数 177 浏览 9 评论 0原文

NSStringcopyretain 有什么区别?

- (void)setString:(NSString*)newString
{
    string = [newString copy];
}

What is the difference between copy and retain for NSString?

- (void)setString:(NSString*)newString
{
    string = [newString copy];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

记忆消瘦 2024-08-31 09:39:54

在一般设置中,保留一个对象会将其保留计数增加一。这将有助于将对象保留在内存中并防止其被吹走。这意味着,如果您仅持有该副本的保留版本,则您可以与将其传递给您的任何人共享该副本。

无论您如何复制对象,都应该创建另一个具有重复值的对象。将此视为克隆。您不会与将其传递给您的任何人共享该克隆。

特别是在处理 NSString 时,您可能无法假设给您一个 NSString 的人确实给了您一个 NSString。有人可能会向您提供一个子类(在本例中为 NSMutableString),这意味着他们可能会在幕后修改值。如果您的应用程序依赖于传入的值,并且有人对您进行了更改,那么您可能会遇到麻烦。

In a general setting, retaining an object will increase its retain count by one. This will help keep the object in memory and prevent it from being blown away. What this means is that if you only hold a retained version of it, you share that copy with whomever passed it to you.

Copying an object, however you do it, should create another object with duplicate values. Think of this as a clone. You do NOT share the clone with whomever passed it to you.

When dealing with NSStrings in particular, you may not be able to assume that whoever is giving you an NSString is truly giving you an NSString. Someone could be handing you a subclass (NSMutableString, in this case) which means that they could potentially modify the values under the covers. If your application depends on the value passed in, and someone changes it on you, you can run into trouble.

画中仙 2024-08-31 09:39:54

保留和复制是两个不同的事情,第一
概念上是按引用调用,而第二个是按值调用。

Retaining and copying are two different things, the first
is conceptually call-by-reference while the second is call-by-value.

绳情 2024-08-31 09:39:54

retain :它是在创建的对象上完成的,它只是增加引用计数。

copy -- 它创建一个新对象,创建新对象时保留计数将为 1。

希望这对您有帮助...:)

retain : It is done on the created object, and it just increase the reference count.

copy -- It creates a new object and when new object is created retain count will be 1.

Hope This Help for U...:)

剪不断理还乱 2024-08-31 09:39:54

这是一篇旧帖子,但这是我对问题 Retain 的看法,

Retain 将对象的保留计数增加 1 并获取对象的所有权。

而复制将复制内存位置中存在的数据并将其分配给变量,因此在复制的情况下,您首先从一个位置复制数据并将其分配给变量,这会增加保留计数。

请记住,保留参考作品和复制作品的价值

Its an old post but here's my view on the question

Retain increases the retain count of an object by 1 and takes ownership of an object.

Whereas copy will copy the data present in the memory location and will assign it to the variable so in the case of copy you are first copying the data from a location assign it to the variable which increases the retain count.

Just remember that retain works on reference and copy works on value

[旋木] 2024-08-31 09:39:54

如果使用retain,它会从原始值复制指针值。retain还会将引用计数增加1。
但在复制的情况下,它复制指针引用的数据并将其分配给副本的实例变量。

if you use retain, it copy the pointer value from original one.retain also increment the reference count by one.
but in case of copy, it duplicate the data referenced by the pointer and assign it to copy's instance variable.

清欢 2024-08-31 09:39:54

最大的区别是,如果你使用copy,你要复制的对象必须实现NSCopying协议(非常容易做到)。并非每个对象都实现了这一点,因此在尝试调用 copy 时,您需要小心地确定要操作的类型(或检查该协议的支持)。

我能想到的使用复制的最佳经验法则是始终将 NSString 属性设置为“复制”而不是保留。这样,如果您搞砸了并忘记释放物体所握住的绳子,您可以从 Leaks 仪器中获得更准确的读数。文案的其他用途需要更仔细地考虑。

The biggest difference is that if you use copy, the object you are copying must implement the NSCopying protocol (very easy to do). Not every object implements that, so you need to use care you know for sure what type you'll be operating against (or check for support of that protocol) when trying to call copy.

The best rule of thumb to using copy I can think of, is to always set NSString properties to "copy" instead of retain. That way you get more accurate readings from the Leaks instrument if you mess up and forget to release a string an object is holding onto. Other uses of copy need to be more carefully thought out.

悲凉≈ 2024-08-31 09:39:54

保留一个对象意味着保留计数加一。这意味着该对象的实例将保留在内存中,直到其保留计数降至零。该属性将存储对此实例的引用,并将与保留该实例的其他任何人共享同一实例。复制意味着对象将被克隆并具有重复的值。它不与任何其他人共享。

Retaining an object means the retain count increases by one. This means the instance of the object will be kept in memory until it’s retain count drops to zero. The property will store a reference to this instance and will share the same instance with anyone else who retained it too. Copy means the object will be cloned with duplicate values. It is not shared with any one else.

撕心裂肺的伤痛 2024-08-31 09:39:54

复制:创建一个新实例,它是接收者的副本。这意味着您将有 2 个不同的

retain: 增加接收器的 retainCount。当retainCount为0时,对象从内存中删除(使用dealloc)。

copy: creates a new instance that's a copy of the receiver. It means that you'll have 2 different

retain: Increases the retainCount of the receiver. An object is removed from memory - (with dealloc), when retainCount is 0.

内心激荡 2024-08-31 09:39:54

指定保留属性,使其可以保留另一个内存,即也可以使其指向另一个地址
copy 首先复制地址,然后保留。

retain attribute is specified such that it can retain the another memory i.e it can be made to point to another address also
copy First copies the address and then retains it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文