alloc+initWithString: 与 copy 相同吗?

发布于 2024-12-19 17:07:01 字数 235 浏览 3 评论 0原文

基本上,问题是 - 以下内容本质上是相同的吗?

NSString *value1 = ...;
NSString *value2 = [[NSString alloc] initWithString:value1];

NSString *value1 = ...;
NSString *value2 = [value1 copy];

Basically, the question is - are the following essentially the same?

NSString *value1 = ...;
NSString *value2 = [[NSString alloc] initWithString:value1];

and

NSString *value1 = ...;
NSString *value2 = [value1 copy];

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

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

发布评论

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

评论(2

逆蝶 2024-12-26 17:07:01

从概念上讲,是的。但是,有一个区别:alloc 始终创建一个新字符串,而 copy 可能返回相同的字符串。

特别是,不可变对象(例如不可变字符串)可能会通过返回自身而不是创建并返回副本来响应复制。 (毕竟,如果您无法更改原始内容的任何内容,为什么您真的需要副本?)可变字符串将通过创建并返回副本来响应它,正如您所期望的那样。

initWithString: 位于中间:它可能会释放接收器并返回您给它的字符串,类似于 copy 返回接收器的方式。但是,如果发生这种情况,则意味着您浪费了使用 alloc 创建的字符串的创建时间。使用copy,您可能根本不需要创建任何其他对象。

使用 allocinitWithString: 的唯一原因是,如果您有自己的 NSString 子类,并且想要从现有字符串创建它的实例。 copy 不会使用您想要的子类。由于在 Cocoa 中几乎从不保证子类化 NSString,因此使用 initWithString: (或 stringWithString:)也是如此。

所以底线是,只需使用copy(或mutableCopy)。它更短,更清楚地表达您的意图,并且可以更快。

Conceptually, yes. However, there is one difference: alloc always creates a new string, whereas copy may return the same string.

In particular, immutable objects, such as immutable strings, are likely respond to copy by returning themselves rather than creating and returning a copy. (After all, if you can't change anything about the original, why would you really need a copy?) Mutable strings will respond to it by creating and returning a copy, as you'd expect.

initWithString: is in the middle: It may release the receiver and return the string you gave it, similar to how copy may return the receiver. However, if that happens, it means you wasted the creation of the string you created with alloc. With copy, you may not need to create any additional objects at all.

About the only reason to use alloc and initWithString: is if you have your own subclass of NSString and want to make an instance of it from an existing string. copy won't use your desired subclass. Since subclassing NSString is practically never warranted in Cocoa, the same is true of using initWithString: (or stringWithString:).

So the bottom line is, just use copy (or mutableCopy). It's shorter, clearer about your intent, and can be faster.

自由如风 2024-12-26 17:07:01

与普通对象相比,不可变字符串的处理有点特殊,所以在这种情况下,是的,这两个操作是相同的。

也就是说:

NSString *str1 = @"string";
NSString *str2 = [str1 copy];
NSString *str3 = [[NSString alloc] initWithString: str1];

NSLog(@"str1: %p, str2: %p, str3: %p", str1, str2, str3);

这给了我以下输出:

str1: 0x108a960b0, str2: 0x108a960b0, str3: 0x108a960b0

由于指针地址相同,所以我们正在谈论同一个对象。

Non-mutable strings are treated a bit special, compared to ordinary objects, so in this case, yes, the two operations are the same.

To wit:

NSString *str1 = @"string";
NSString *str2 = [str1 copy];
NSString *str3 = [[NSString alloc] initWithString: str1];

NSLog(@"str1: %p, str2: %p, str3: %p", str1, str2, str3);

Which gives me the following output:

str1: 0x108a960b0, str2: 0x108a960b0, str3: 0x108a960b0

Since the pointer addresses are the same, we are talking about the same object.

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