2017-03-17 63 views
0

對象我有一個類A和子類B存檔/取消存檔父子用鑰匙

@interface A:NSObject<NSCoding> 
String a; 
String b; 
@end 
@interface B: A<NSCoding> 
int c; 
// `a` and `b` attributes are inherited 
@end; 

我要存檔的A一些對象,並使用屬性a作爲key一些B。 在兩個類中的encodingdecoding協議已經被定義爲:

@implementation A 
... 
- (void) encodeWithCoder:(NSCoder *)encoder { 
    //[super encodeWithCoder:encoder]; 
    [encoder encodeObject:self forKey:a]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    // self = [super initWithCoder:decoder]; 
    if (self=[super init]) { 
     self = [decoder decodeObjectForKey:a]; 
    } 
    return self; 
} 
@end 

@implementation B 
.. 
- (void) encodeWithCoder:(NSCoder *)encoder { 
    [super encodeWithCoder:encoder]; 
    [encoder encodeObject:self forKey:super.a]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    //self = [super initWithCoder:decoder]; 
    if (self) { 
     self = [decoder decodeObjectForKey:super.a]; 
    } 
    return self; 
} 

對於一些對象列表dataArray = [A("One"), B("Two")],歸檔邏輯是

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataArray]; 
      NSError* error = nil; 
      BOOL success = [data writeToFile: @"xyz.dat" options:0 error: &error]; 
      if (!success) 
       NSLog(@"error = %@", [error description]); 

再從歸檔數據讀取:

NSData *lD = [[NSData alloc] initWithContentsOfFile:@"xyz.dat"]; 
NSKeyedUnarchiver *arc = [[NSKeyedUnarchiver alloc] initForReadingWithData:lD]; 

現在將屬性爲a的對象設置爲"One" 在嘗試從歸檔數據中讀取對象時,我得到了null

NSLog(@"Value - %@",[arc decodeObjectForKey:@"One"]); 
+0

檢查該鏈接的http://計算器。 COM /問題/ 41116832 /時,歸檔對象中最FUNC-encodewith-acoder-nscoder-方法 - 崩潰,使用/ 41116908#41116908 –

回答

0

也許,問題出在編碼self。 我做了以下修改:

@implementation A 
... 
- (void) encodeWithCoder:(NSCoder *)encoder { 
    if(self.a!=nil) 
     [encoder encodeObject:self.a forKey:@"a"]; 
    if(self.b!=nil) 
     [encoder encodeObject:self.b forKey:@"b"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    self=[super init]; 
    if (self!=nil) { 
     self.a= [decoder decodeObjectForKey:@"a"]; 
     self.b= [decoder decodeObjectForKey:@"b"]; 
    } 
    return self; 
} 
@end 

@implementation B 
.. 
- (void) encodeWithCoder:(NSCoder *)encoder { 
    [super encodeWithCoder:encoder ]; 
    [encoder encodeObject:self.c forKey:@"c"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    if ((self = [super initWithCoder:decoder ])) 
    { 
     self.c= [decoder decodeObjectForKey:@"c"]; 
    } 
    return self; 
} 

雖然編碼數據:

NSMutableDictionary *dict = [NSMutableDictionary new]; 
[dict setObject:[[A alloc]init()] forKey:@"One"];//say 
[dict setObject:[[B alloc]init()] forKey:@"Two"];//say 

NSMutableData *yourData = [[NSMutableData alloc] init]; 
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:yourData]; 
for (NSString* key in dict) { 
    id value = [dict objectForKey:key]; 
    [archiver encodeObject:value forKey: key]; 
} 
[ archiver finishEncoding]; 
[yourData writeToFile:@"xyz.dat" atomically:YES]; 
[yourData release]; 
[archiver release]; 

和解碼:

NSData *data = [[NSData alloc] initWithContentsOfFile:@"xyz.dat"]; 
NSKeyedUnarchiver *keyArc= [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 
NSLog(@"%@",[keyArc decodeObjectForKey:@"One"]); 
NSLog(@"%@",[keyArc decodeObjectForKey:@"Two"]); 
[keyArc finishDecoding]; 

編號:https://stackoverflow.com/a/10929284/2301570