分配和释放VS。自动释放。为什么以及何时?
我看到很多人都在分配和释放 NSString。
我知道这样做的好处是字符串会立即释放,而不是通过 autoRelease 释放。
我的问题:
- 它有效吗?我应该总是更喜欢在 autoRelease 上分配和释放吗?
- 更广泛的是-立即分配和释放,然后再次分配或在dealloc中分配、使用和释放。
将不胜感激任何解释。
谢谢沙尼
I see that many people are allocating and releasing NSStrings.
I understand that the benefit is that the string is being released immediately and not by autoRelease.
my questions :
- does it effective and should i always prefer allocating and releasing on autoRelease?
- what is more expansive - allocating and releasing immediately and then allocating again OR allocating, using and releasing in dealloc.
will appreciate any explanation.
Thanks
shani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSAutoreleasePool
pool.我不明白如何重用 NSString。重用 NSMutableString 实例可能比重新创建它稍快一些,但您不会看到差异。专注于代码的简单性和可维护性。
我认为你的问题是使用 [[NSString alloc] init] 还是 [NSString string]。只要性能不是问题,就始终选择最简单的。这将是自动发布的版本,因为您不需要自己发布它。
I don't see how you could re-use a NSString. Reusing a NSMutableString instance might be slightly faster the recreating it but you won't see the difference. Focus on simplicity and maintainability of your code.
I think your question is wether to use [[NSString alloc] init] or [NSString string]. As long as performance is not an issue, always go with the simplest one. That would be the autoreleased version, because you don't need to release it yourself.