这两个 NSString 方法之间的区别
所以我今天在一次采访中被问到这个问题,经过一番谷歌搜索后我仍然无法找到答案(事实上我什至根本找不到任何使用 [NSString string] 方法的代码)。
之间有什么区别
NSString *someString = [NSString string];
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
NSString *someString = [NSString string];
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是您的回答吗?您是否问过为什么您的回答不正确?我问是因为你的假设大部分是正确的(在更高的层面上)。
从
alloc+init
返回时,它并不完全是“保留”的,它是一个您持有一个引用的对象,并且应该与release
或autorelease
保持平衡代码>.对于方便的构造函数(+[NSString string]
),您将返回一个对象,该对象持有零个引用,但您可以期望该对象一直存在,直到弹出当前自动释放池,除非您发送它显式保留(假设 MRC 或 ARC,因为它被标记为 iOS)。在较低级别,你可以做出一些猜测,但我不希望在许多 objc 面试中出现这个问题(除非你告诉他们你是中级或高级)。基本上,它是实现定义的,但两种形式都可以返回相同的静态常量 NSString(这可能是面试官正在寻找的)。为了说明这一点:
同样,这是实现定义的,但是是明显的优化。此外,在某些情况下,这种优化使得对可变变体 (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 arelease
orautorelease
. 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: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.
从技术上讲,它是一个恒定的占位符字符串,即它在整个程序执行过程中都存在,永远不会被释放。它不是自动释放的字符串。从概念上讲,这是我作为面试官所关注的重点,它是一个不属于调用者所拥有的字符串(空字符串),因此调用者不应该释放它。
从技术上讲,它是一个常量的占位符字符串,即它在整个程序执行过程中都存在。其实和上面是同一个对象,没有保留。从概念上讲,这是我作为面试官关注的重点,它是调用者拥有的字符串(空字符串),因此调用者负责在不再需要时释放它。
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.
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.
正确的答案是,
给你一个你不拥有的空字符串,你不能释放它(根据内存管理规则),
而
给你一个你拥有的空字符串,你必须释放它(根据内存管理规则) )。
如果不深入研究实现,您就无法对这两个字符串说任何其他内容。你不能说它们是自动释放的,因为它们可能不是,而且你不能说保留计数是多少。
事实上,您可能会(在这两种情况下)获得指向某个 NSString 子类的常量对象的相同指针,可能具有
UINT_MAX
的保留计数,运行时将其用作标志禁用常量字符串的正常保留释放行为。我实际上并没有尝试过上面的方法,因为除了 Objective-C SDK 的维护者之外没有人需要关心。The correct answer is that
gives you an empty string that you do not own and that you must not release (according to the memory management rules)
whereas
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.您不经常看到
相同
,因为它与较短的 。它通常用于创建一个空的 NSMutableString
You don't often see
because it's the same as
which is shorter. It's usually used to create an empty NSMutableString
我唯一能想象的是:
不会分配内存,因为它不是用 alloc 创建的。它是系统创建的常量(空字符串),不需要释放。
您自己为 NSString 分配内存,这意味着您必须在使用完毕后跟踪 NSString 是否仍然“存活”,因此需要释放它。
The only thing I can imagine is that:
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.
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.