-
You should always use
[[self class] allocWithZone:zone]
to make sure you are creating a copy using the appropriate class. The example you give for 002 shows exactly why: Subclasses will call[super copyWithZone:zone]
and expect to get back an instance of the appropriate class, not an instance of the super class. -
I access the ivars directly, so I don't need to worry about any side effects I might add to the property setter (e.g., generating notifications) later on. Keep in mind, subclasses are free to override any method. In your example, you are sending two extra messages per ivar. I would implement it as follows:
Code:
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [super copyWithZone:zone];
newCrime->_month = [_month copyWithZone:zone];
newCrime->_category = [_category copyWithZone:zone];
// etc...
return newCrime;
}
Of course, whether you copy the ivars, retain them, or just assign them should mirror what the setters do.