这两个 NSString 方法之间的区别

发布于 2024-12-12 08:57:56 字数 526 浏览 1 评论 0原文

所以我今天在一次采访中被问到这个问题,经过一番谷歌搜索后我仍然无法找到答案(事实上我什至根本找不到任何使用 [NSString string] 方法的代码)。

之间有什么区别

  1. NSString *someString = [NSString string];
  2. NSString *someString = [[NSString alloc] init];

现在我最初的想法是 [ NSString string] 将返回一个将自动释放的对象,而使用 alloc 和 init 将返回一个已保留的对象。然而这个答案似乎是不正确的。

我查看了苹果文档中的 NSString 类参考,但它所说的只是

Returns an empty string.

+ (id)string 

Return Value
An empty string.

有人可以向我解释一下这两者之间的区别是什么吗?

So I just got asked this at an interview today and after some googling am still unable to figure out the answer (in fact I couldn't even find any code at all which used the [NSString string] method).

What is the difference between

  1. NSString *someString = [NSString string];
  2. NSString *someString = [[NSString alloc] init];

Now my initial thoughts were that [NSString string] would return an object which would be autoreleased whereas using alloc and init would return an object which has been retained. However it seems that this answer was incorrect.

I've looked at the NSString class reference in the apple docs but all it says is

Returns an empty string.

+ (id)string 

Return Value
An empty string.

Could somebody explain to me exactly what the difference between these two are?

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

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

发布评论

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

评论(5

甜心小果奶 2024-12-19 08:57:56

这是您的回答吗?您是否问过为什么您的回答不正确?我问是因为你的假设大部分是正确的(在更高的层面上)。

alloc+init 返回时,它并不完全是“保留”的,它是一个您持有一个引用的对象,并且应该与 releaseautorelease 保持平衡代码>.对于方便的构造函数(+[NSString string]),您将返回一个对象,该对象持有零个引用,但您可以期望该对象一直存在,直到弹出当前自动释放池,除非您发送它显式保留(假设 MRC 或 ARC,因为它被标记为 iOS)。

在较低级别,你可以做出一些猜测,但我不希望在许多 objc 面试中出现这个问题(除非你告诉他们你是中级或高级)。基本上,它是实现定义的,但两种形式都可以返回相同的静态常量 NSString(这可能是面试官正在寻找的)。为了说明这一点:

@implementation NSString

static NSString * const EmptyNSString = @"";

- (id)init
{
    self = [super init];
    [self release];
    return EmptyNSString;
}

+ (id)string
{
    return EmptyNSString;
}

...

同样,这是实现定义的,但是是明显的优化。此外,在某些情况下,这种优化使得对可变变体 (NSMutableString) 的具体不可变类型 (NSString) 进行物理子类化变得困难。

Was that your response, and did you ask why your answer was incorrect? I ask because your assumption is mostly correct (at a higher level).

It's not exactly 'retained' when returned from alloc+init, it is an object you hold one reference to, and should balance with a release or autorelease. For the convenience constructor (+[NSString string]), you are returned an object which you hold zero references to, but one which you can expect to live until the current autorelease pool is popped unless you send it an explicit retain (assuming MRC or ARC, since it is tagged iOS).

At the lower level, you could make some guesses, but I wouldn't expect that question in many objc interviews (unless you told them you were mid or senior level). Basically, it is implementation defined, but both forms could return the same static, constant NSString (that may have been what the interviewer was looking for). To illustrate:

@implementation NSString

static NSString * const EmptyNSString = @"";

- (id)init
{
    self = [super init];
    [self release];
    return EmptyNSString;
}

+ (id)string
{
    return EmptyNSString;
}

...

Again, that's implementation defined, but an obvious optimization. As well, that optimization makes physically subclassing concrete immutable types (NSString) difficult for mutable variants (NSMutableString) in some cases.

乖乖 2024-12-19 08:57:56

现在我最初的想法是 [NSString string] 将返回一个自动释放的对象

从技术上讲,它是一个恒定的占位符字符串,即它在整个程序执行过程中都存在,永远不会被释放。它不是自动释放的字符串。从概念上讲,这是我作为面试官所关注的重点,它是一个不属于调用者所拥有的字符串(空字符串),因此调用者不应该释放它。

而使用 alloc 和 init 将返回一个已保留的对象

从技术上讲,它是一个常量的占位符字符串,即它在整个程序执行过程中都存在。其实和上面是同一个对象,没有保留。从概念上讲,这是我作为面试官关注的重点,它是调用者拥有的字符串(空字符串),因此调用者负责在不再需要时释放它。

Now my initial thoughts were that [NSString string] would return an object which would be autoreleased

Technically, it’s a placeholder string that is constant, i.e., it lives throughout the entire program execution, never being released. It’s not an autoreleased string. Conceptually, and this is what I’d focus as an interviewer, it’s a string (an empty string) that is not owned by the caller, hence the caller shouldn’t release it.

whereas using alloc and init would return an object which has been retained

Technically, it’s a placeholder string that is constant, i.e., it lives throughout the entire program execution. In fact, it’s the same object as the one above, and it is not retained. Conceptually, and this is what I’d focus as an interviewer, it’s a string (an empty string) that is owned by the caller, hence the caller is responsible for releasing it when it’s not needed any longer.

晨曦慕雪 2024-12-19 08:57:56

正确的答案是,

NSString *someString = [NSString string];

给你一个你不拥有的空字符串,你不能释放它(根据内存管理规则),

NSString *someString = [[NSString alloc] init];

给你一个你拥有的空字符串,你必须释放它(根据内存管理规则) )。

如果不深入研究实现,您就无法对这两个字符串说任何其他内容。你不能说它们是自动释放的,因为它们可能不是,而且你不能说保留计数是多少。

事实上,您可能会(在这两种情况下)获得指向某个 NSString 子类的常量对象的相同指针,可能具有 UINT_MAX 的保留计数,运行时将其用作标志禁用常量字符串的正常保留释放行为。我实际上并没有尝试过上面的方法,因为除了 Objective-C SDK 的维护者之外没有人需要关心。

The correct answer is that

NSString *someString = [NSString string];

gives you an empty string that you do not own and that you must not release (according to the memory management rules)

whereas

NSString *someString = [[NSString alloc] init];

gives you an empty string you do own and that you must release (according to the memory management rules).

Without poking into the implementation, you can't say anything else about those two strings. You can't say that they are autoreleased, because they might not be and you can't say what the retain count will be.

In actual fact, you'll probably get (in both cases) the same pointer to a constant object of some NSString subclass, probably with a retain count of UINT_MAX which is used by the run time as a flag to disable normal retain release behaviour for constant strings. I haven't actually tried the above because nobody except the maintainers of the Objective-C SDK needs to care.

真心难拥有 2024-12-19 08:57:56

您不经常看到

NSString *someString = [NSString string]; 

相同

NSString *someString = @""; 

,因为它与较短的 。它通常用于创建一个空的 NSMutableString

NSMutableString* s = [NSMutableString string];

You don't often see

NSString *someString = [NSString string]; 

because it's the same as

NSString *someString = @""; 

which is shorter. It's usually used to create an empty NSMutableString

NSMutableString* s = [NSMutableString string];
粉红×色少女 2024-12-19 08:57:56

我唯一能想象的是:

  1. 不会分配内存,因为它不是用 alloc 创建的。它是系统创建的常量(空字符串),不需要释放。

  2. 您自己为 NSString 分配内存,这意味着您必须在使用完毕后跟踪 NSString 是否仍然“存活”,因此需要释放它。

The only thing I can imagine is that:

  1. Won't allocate memory since it is not made with alloc. It is a constant (an empty string) made by the system and doesn't need to be released.

  2. You allocate the memory for the NSString yourself which means you have to keep track if the NSString still 'lives' or not when you are done with it, and thus need to release it.

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