[NSMutableString string] 与 [[NSMutableString string] autorelease] 相同吗?

发布于 2024-12-20 16:47:52 字数 142 浏览 4 评论 0原文

我注意到某处有一段代码 NSMutableString *myString = [[NSMutableString string] autorelease];

是不是太过分了?它不应该和 [NSMutableString string] 一样吗?

I noticed a piece of code somewhere it does
NSMutableString *myString = [[NSMutableString string] autorelease];

Is it overkill? Shouldn't it be the same as [NSMutableString string]?

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

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

发布评论

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

评论(2

柏拉图鍀咏恒 2024-12-27 16:47:52
NSMutableString *myString = [[NSMutableString string] autorelease];

如果他们不在其他地方调用保留,将导致崩溃。

[NSMutableString string]

是一样的

[[[NSMutableString alloc] init] autorelease]
NSMutableString *myString = [[NSMutableString string] autorelease];

will lead to a crash if they aren't calling retain on it elsewhere.

[NSMutableString string]

is the same as

[[[NSMutableString alloc] init] autorelease]
鱼窥荷 2024-12-27 16:47:52

我觉得不太对劲。您确定您看到的示例实际上是这样做的吗?

[NSMutableString string] 相当于

[[[NSMutableString alloc] init] autorelease]

因此,根据您提供的示例,您将得到

[[[[NSMutableString alloc] init] autorelease] autorelease]

这将导致过度释放和 exc_bad_access 错误。

Doesn't look right to me. Are you sure that the example you saw is actually doing this?

[NSMutableString string] is equivalent to

[[[NSMutableString alloc] init] autorelease]

So with the example you provided, you would get

[[[[NSMutableString alloc] init] autorelease] autorelease]

which would result in an over release and an exc_bad_access error.

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