Best Practice beim Implementieren von copyWithZone:

Ich versuche zu klären ein paar Dinge im Kopf, über die Umsetzung copyWithZone: kann jeder Kommentar über die folgenden ...

//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:

  1. Ist es am besten zu schreiben, den Namen der Klasse direkt [[Crime allocWithZone:zone] init] oder sollte ich [[[self Class] allocWithZone:zone] init]?
  2. Ist es in Ordnung [self month] für das kopieren des iVars oder sollte ich auf den iVars direkt, d.h. _month?

InformationsquelleAutor der Frage fuzzygoat | 2012-03-28

Schreibe einen Kommentar