What is difference between alloc and allocWithZone:?

It wouldn't surprise me if I get downvoted for this answer, since I've never used the method and I'm also interested to hear what others say, but from the documentation.

It wouldn't surprise me if I get downvoted for this answer, since I've never used the method and I'm also interested to hear what others say, but from the documentation: 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. 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; }.

Yes,that is a good example to use allocWithZone: – Forrest Dec 23 '10 at 2:49 9 Correct. The reality, though, is that pretty much nothing uses zones. It was originally intended to give the ability to, say, co-locate all of the objects related to a document in a zone and then bulk destroy them by simply deallocating the zone.In practice, this proved unworkable and zones haven't been used in more than a decade in the OpenStep APIs.

– bbum Dec 23 '10 at 3:01 Thanks for that. Very informative. I figured given the lack of focus on them in the documentation that they're not exactly used much.

– d11wtq Dec 23 '10 at 6:00.

A good example for using allocWithZone: is when you are implementing the NSCopy protocol, which allows you make your custom objects copyable (deep copy / copy by value) like: (1) ClassName *newObject = currentObject copy; //results in newObject being a copy of currentObject not just a reference to it The NSCopy protocol ensures you implement a method: (2) -(id)copyWithZone:(NSZone *)zone; When copying an object the 'copy' message you send as above (1) is converted to a 'copyWithZone:' message behind the scenes and calls your method (2). Aka you don't have to do anything to get a zone yourself. Now as you have a 'zone' sent to this message you can use it to ensure a copy is made from memory in the same region as the original.

This can be used like: -(id)copyWithZone:(NSZone *)zone { newCopy = self classallocWithZone:zoneinit; //gets the class of this object then allocates a new object close to this one and initialises it before returning return(newCopy); } This is the only place I am aware allocWithZone is actually used.

According to Alvaro Polo, answering the question 'de instance variable from header file in Objective C' (stackoverflow.com/questions/2103858/hide...) overriding allocWithZone, in classes derived from NSObject, is a very good way of hiding instance variables: create an implementation class inside your implementation file and allow it to own the instance variables. The overridden allocWithZone creates an implementation class object.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions