Objective-C - alloc 和 allocFromZone 之间的区别?
我读了一篇文章提到使用 allocWithZone
通过使用更靠近对象本身的内存位置来帮助提高性能。这是真的吗?是否建议使用allocWithZone
而不是alloc
? alloc 和 allocWithZone 有什么区别?
证明的例子是:
- (id)init
{
if (self = [super init])
{
_myString = [[NSString allocWithZone:[self zone] init]];
}
return self;
}
Possible Duplicate:
what is difference between alloc and allocWithZone:?
I read an article that mentions using allocWithZone
helps improve performance by using a memory location closer to the object itself. Is this true? and is it recommended to use allocWithZone
instead of alloc
? What is the difference between alloc and allocWithZone?
The example proveded was:
- (id)init
{
if (self = [super init])
{
_myString = [[NSString allocWithZone:[self zone] init]];
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个 ARC 不允许在对象上调用
zone
。由于所有事情都在朝着这个方向发展,这似乎是不使用allocWithZone 的充分理由。据我所知,使用
allocWithZone
有一个很好的用途,它符合NSCopying
。虽然您可以完全忽略区域参数,但我喜欢做类似的事情:
除此之外,我认为没有什么比发布的 上一个问题。
First ARC disallows calling
zone
on objects. Since all things are moving that way that seems a good enough reason to not useallocWithZone
.There is one good use that I know of to use
allocWithZone
, conforming toNSCopying
.And while you can ignore the zone parameter alltogether, I like to do something like:
Other than that I don't think there's much more to say than is posted the previous question.
直接来自 马嘴:
(这是来自 ARC 发行说明,对于在 Apple 再次破坏其文档结构后阅读本文的任何人来说。)
内存区域曾经是有用的,但 Cocoa 已经彻底放弃了它们。 NSZone就像一条残留的尾巴,ARC和libauto GC都把它剪掉了。正如 Apple 工程师 Bill Bumgarner 在对 bryanmac 欺骗行为的评论中指出的那样,它们只是笨拙且在 Cocoa 中没有发挥出很大的作用。
Straight from the horse's mouth:
(That's from the ARC release notes, for anyone reading this after Apple breaks their documentation structure again.)
Memory zones were somewhat useful once upon a time, but Cocoa has pretty thoroughly moved away from them. NSZone is like a vestigial tail, and both ARC and the libauto GC cut it off. As Apple engineer Bill Bumgarner pointed out in the comments on bryanmac's dupe, they were simply unwieldy and not used to great effect in Cocoa.