iPhone:(id)copyWithZone:(NSZone *)区域:什么是“区域”对于? [英] iPhone : (id)copyWithZone:(NSZone *)zone : what is "zone" for?
问题描述
在类中实现此NSCopying方法以启用复制时,区域参数的用途是什么?
如果我设置一个新对象,我不需要用allocWithZone分配它,因为一个分配就够了......我很困惑......
When implementing this method of NSCopying in a class to enable copy, what is the zone param use ? If I set a new object, I do not need to alloc it with allocWithZone as an alloc is just enough... I'm confused...
推荐答案
这是旧时代的遗物,我们有多个区域可供分配。这些天,所有应用程序只有一个区域,所有分配都在这里,但是 NSZone
类仍然存在,编写的代码太多依赖于 + allocWithZone:
作为进行更改的基本分配方法。
It's a relic from the old days, where we had multiple "zones" to allocate in. These days, all apps only have a single zone where all allocations are made, but the NSZone
class still exists and far too much code is written to depend on +allocWithZone:
being the fundamental allocation method to make the change.
简而言之,您可以完全忽略 NSZone
结构,并且唯一理由关注 + allocWithZone:
是否需要覆盖它。与 -copyWithZone:
类似,您可以忽略该区域。如果您愿意,可以调用 + allocWithZone:
传入相同的区域,但不会有任何区别。
In short, you can ignore the NSZone
struct in its entirety, and the only reason to care about +allocWithZone:
is if you need to override it. Similarly with -copyWithZone:
, you can just ignore the zone. If you so desire, you can call +allocWithZone:
passing in the same zone, but it won't make any difference.
这篇关于iPhone:(id)copyWithZone:(NSZone *)区域:什么是“区域”对于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!