alloc和allocWithZone之间有什么区别? [英] what is difference between alloc and allocWithZone:?
问题描述
在论坛讨论中,似乎最大的区别是性能因素, allocWithZone:将从特定的内存区域分配内存,从而降低交换成本.
From forum discussion , seem like that the big difference is performance factor, allocWithZone: will alloc memory from particular memory area, which reduce cost of swapping.
在实践中,几乎没有机会使用allocWithZone:,任何人都可以举一个简单的示例来说明使用allocWithZone:的哪种情况?
In practice, almost get no chance to use allocWithZone: , anyone can give simple example to illustrate which case to use allocWithZone: ?
谢谢
推荐答案
当一个对象创建另一个对象时, 有时要确保一个好主意 他们都是从同一个分配的 内存区域.区域法 (在NSObject协议中声明) 可用于此目的;它 返回接收者所在的区域 找到.
When one object creates another, it’s sometimes a good idea to make sure they’re both allocated from the same region of memory. The zone method (declared in the NSObject protocol) can be used for this purpose; it returns the zone where the receiver is located.
这向我建议,您的ivars和您的类创建"的任何对象本身都可以使用+allocWithZone:
来使它们在同一区域中创建的实例.
This suggests to me that your ivars, and any objects your classes "create" themselves could make use of +allocWithZone:
in this way, to make the instances they create in the same zone.
-(id)init {
if (self = [super init]) {
someIvar = [[SomeOtherClass allocWithZone:[self zone]] init];
}
return self;
}
这篇关于alloc和allocWithZone之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!