Я пытаюсь прояснить некоторые вещи в моей голове о реализации copyWithZone:, может ли кто-нибудь прокомментировать следующее...
// 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;
}
В 001:
-
Лучше ли писать имя класса непосредственно
[[Crime allocWithZone:zone] init]или использовать[[[self Class] allocWithZone:zone] init]? -
Можно ли использовать
[self month]для копирования iVars или я должен напрямую обращаться к iVars, т.е._month?