A good example of using allocWithZone is when you implement the NSCopy protocol, which allows you to copy your custom objects (deep copy / copy by value), for example:
(1) ClassName *newObject = [currentObject copy];
The NSCopy protocol provides an implementation of the method:
(2) -(id)copyWithZone:(NSZone *)zone;
When copying an object, the message "copy" is transmitted, as described above (1), when it is specified as "copyWithZone" sends a message to method (2). otherwise you do not need to do anything to get the zone yourself.
Now that you have a βzoneβ sent to this message, you can use it to ensure that a copy is made from memory in the same region as the original.
This can be used as:
-(id)copyWithZone:(NSZone *)zone { newCopy = [[[self class]allocWithZone:zone]init];
This is the only place where I know that allocWithZone is actually used.
Richard Washington Nov 18 2018-11-11T00: 00Z
source share