Objective-C - alloc 和 allocFromZone 之间的区别?

发布于 2024-12-15 12:05:14 字数 546 浏览 3 评论 0原文

可能的重复:
alloc 和 allocWithZone 之间有什么区别:?

我读了一篇文章提到使用 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 技术交流群。

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

发布评论

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

评论(2

眼睛会笑 2024-12-22 12:05:14

第一个 ARC 不允许在对象上调用 zone。由于所有事情都在朝着这个方向发展,这似乎是不使用allocWithZone 的充分理由。

据我所知,使用allocWithZone有一个很好的用途,它符合NSCopying

@protocol NSCopying
- (id)copyWithZone:(NSZone *)zone;
@end

虽然您可以完全忽略区域参数,但我喜欢做类似的事情:

-(id)copyWithZone:(NSZone *)zone{
    SampleClass *copy = [[SampleClass allocWithZone:zone] init];
    copy->_myString = [_myString copyWithZone:zone];
    return copy;
}

除此之外,我认为没有什么比发布的 上一个问题

First ARC disallows calling zone on objects. Since all things are moving that way that seems a good enough reason to not use allocWithZone.

There is one good use that I know of to use allocWithZone, conforming to NSCopying.

@protocol NSCopying
- (id)copyWithZone:(NSZone *)zone;
@end

And while you can ignore the zone parameter alltogether, I like to do something like:

-(id)copyWithZone:(NSZone *)zone{
    SampleClass *copy = [[SampleClass allocWithZone:zone] init];
    copy->_myString = [_myString copyWithZone:zone];
    return copy;
}

Other than that I don't think there's much more to say than is posted the previous question.

牵强ㄟ 2024-12-22 12:05:14

直接来自 马嘴

不再需要使用 NSZone ——现代 Objective-C 运行时都会忽略它们

(这是来自 ARC 发行说明,对于在 Apple 再次破坏其文档结构后阅读本文的任何人来说。)

内存区域曾经是有用的,但 Cocoa 已经彻底放弃了它们。 NSZone就像一条残留的尾巴,ARC和libauto GC都把它剪掉了。正如 Apple 工程师 Bill Bumgarner 在对 bryanmac 欺骗行为的评论中指出的那样,它们只是笨拙且在 Cocoa 中没有发挥出很大的作用。

Straight from the horse's mouth:

There is no need to use NSZone any more—they are ignored by the modern Objective-C runtime anyway

(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.

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