Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Assuming deep copy is what you want) for the copy we want to make, use copyWithZone: for object instance variables and simply set primitive instance variables using =.</p> <pre><code>- (id)copyWithZone:(NSZone *)zone { MyClass *copy = [[MyClass alloc] init]; // deep copying object properties copy.objectPropertyOne = [[self.objectPropertyOne copyWithZone:zone] autorelease]; copy.objectPropertyTwo = [[self.objectPropertyTwo copyWithZone:zone] autorelease]; ... copy.objectPropertyLast = [[self.objectPropertyLast copyWithZone:zone] autorelease]; // deep copying primitive properties copy.primitivePropertyOne = self.primitivePropertyOne copy.primitivePropertyTwo = self.primitivePropertyTwo ... copy.primitivePropertyLast = self.primitivePropertyLast // deep copying object properties that are of type MyClass copy.myClassPropertyOne = self.myClassPropertyOne copy.myClassPropertyTwo = self.myClassPropertyTwo ... copy.myClassPropertyLast = self.myClassPropertyLast return copy; } </code></pre> <p>But notice how properties of the same class as self and copy must be set without copyWithZone:. Otherwise, these objects will call this copyWithZone again, and will try to set their myClassProperties using copyWithZone as well. This triggers an unwanted infinite loop. (Also, you could call allocWithZone: instead of alloc: but I'm pretty sure alloc: calls allocWithZone: anyway)</p> <p>There are some cases where using = to deep copy object properties of the same class might not be the thing you want to do, but in ALL CASES (as far as I'm aware) deepcopying object properties of the same class with copyWithZone: or anything that calls copyWithZone: will result in an infinite loop.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload