2010-02-02 10 views

risposta

33

Dovrai implementare lo NSCoding protocol. Implementa initWithCoder: e encodeWithCoder: e la tua classe personalizzata funzionerà con NSKeyedArchiver e NSKeyedUnarchiver.

tuo initWithCoder: dovrebbe essere simile a questo:

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if(self = [super init]) // this needs to be [super initWithCoder:aDecoder] if the superclass implements NSCoding 
    { 
     aString = [[aDecoder decodeObjectForKey:@"aString"] retain]; 
     anotherString = [[aDecoder decodeObjectForKey:@"anotherString"] retain]; 
    } 
    return self; 
} 

e encodeWithCoder:

- (void)encodeWithCoder:(NSCoder *)encoder 
{ 
    // add [super encodeWithCoder:encoder] if the superclass implements NSCoding 
    [encoder encodeObject:aString forKey:@"aString"]; 
    [encoder encodeObject:anotherString forKey:@"anotherString"]; 
} 
+1

+1 Potrebbe essere necessario chiamare '[initWithCoder super: aDecoder]' o '[super encodeWithCoder: encoder ] a seconda di quale classe stai sottoclassi. =) –

+0

Doh! Bella scoperta - risolto. –

+0

Ho una domanda sulle migliori pratiche ... Devo rendere la classe serializzabile o posso lasciarlo allo sviluppatore dell'interfaccia utente per serializzarlo in qualsiasi output desiderato? – Patricia

Problemi correlati