之间的区别是什么

之间的区别是什么

本文介绍了“复制”与“复制”之间的区别是什么?和“保留”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

copy 与保留之间的区别是 NSString

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


推荐答案

对象将其保留计数增加1。这将有助于保持对象在内存中,并防止它被吹走。这意味着,如果您只持有保留的版本,您可以与他人分享副本。

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.

特别在处理 NSString 时,可能无法假定谁给你一个 NSString 真的给你一个 NSString 。有人可能会给你一个子类( NSMutableString ,在这种情况下),这意味着他们可能修改覆盖下的值。如果您的应用程序取决于传入的值,并且有人对您进行更改,您可能会遇到麻烦。

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.

这篇关于“复制”与“复制”之间的区别是什么?和“保留”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 13:02