2017-03-17 29 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