iPhone : (id)copyWithZone:(NSZone *)zone : what is "zone" for?
Asked Answered
L

2

16

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

Lamm answered 8/1, 2011 at 0:24 Comment(0)
D
25

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.

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.

Duplicature answered 8/1, 2011 at 0:34 Comment(0)
S
1

Have a look over the NSCopying Protocol Reference, specifically copyWithZone:

Succinylsulfathiazole answered 8/1, 2011 at 0:33 Comment(2)
In my opinion at least, the documentation doesn't explain what it is in any useful way: "an area of memory from which to allocate for the new instance" is vague at best.Illconsidered
Its not explained because its not useful anymore. Back in the old days you often had diferent regions of memory , or "Zones" you could use to store stuff in, and zone stuff was all about that. Nowdays, modern Operating systems let you think about everything as a big ol' pool of stuff anything can access Though in theory there are uses for Zones still (Ie security mapped memory spaces, custom memory mapped file shenanigans etc. But I'm not sure if you can even rely on zones even working anymore.Tiling

© 2022 - 2024 — McMap. All rights reserved.