I am trying to clear up a few things in my head about implementing copyWithZone:
, can anyone comment on the following ...
// 001: Crime is a subclass of NSObject.
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [[[self class] allocWithZone:zone] init];
if(newCrime) {
[newCrime setMonth:[self month]];
[newCrime setCategory:[self category]];
[newCrime setCoordinate:[self coordinate]];
[newCrime setLocationName:[self locationName]];
[newCrime setTitle:[self title]];
[newCrime setSubtitle:[self subtitle]];
}
return newCrime;
}
// 002: Crime is not a subclass of NSObject.
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [super copyWithZone:zone];
[newCrime setMonth:[self month]];
[newCrime setCategory:[self category]];
[newCrime setCoordinate:[self coordinate]];
[newCrime setLocationName:[self locationName]];
[newCrime setTitle:[self title]];
[newCrime setSubtitle:[self subtitle]];
return newCrime;
}
In 001:
Is it best to write the class name directly
[[Crime allocWithZone:zone] init]
or should I use[[[self Class] allocWithZone:zone] init]
?Is it ok to use
[self month]
for copying the iVars or should I be accessing the iVars directly i.e._month
?
NSCopying
. For example,NSObject
doesn't, so calling[super copyWithZone: zone]
will throw an exception. – Electrophorus