Here is the code:
- (id)copyWithZone:(NSZone*)zone {
ExecutedOrderInfo* copy = [[self class] allocWithZone:zone];
copy.executedPrice = self.executedPrice;
copy.executedQuantity = self.executedQuantity;
return (id)copy;
}
The question is, is it necessary to release “copy” in above code? Or release it when someone called it?
No, not in this method. Methods that start with
copymust return non-autorleased objects with retain count 1. Just as you do.PS: the cast in
return (id)copyis not needed.idis the abstract object type and much more general than your concrete class. Casts are only needed when having a concrete class that should be treated as a different one — like a subclass after doing a subclass check.PPS: Your method lacks an
init. It’s not good to justalloca instance. Instead do something like this:[[[self class] allocWithZone:zone] init];