Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 202
Oliver Avatar asked Jan 08 '11 00:01

Oliver


People also ask

What is copywithzone?

Returns a new instance that's a copy of the receiver. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+ Required.

What is Nscopying?

A protocol that objects adopt to provide functional copies of themselves.


1 Answers

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.

like image 121
Lily Ballard Avatar answered Oct 10 '22 03:10

Lily Ballard