decodeObjectForKey:Custom对象中的数组返回nil

时间:2014-08-16 05:44:42

标签: ios objective-c nscoding

我想存储自定义对象的数组。所以我尝试过编码和解码。但它不起作用

请检查我的代码。

    - (IBAction)saveFile{
    NSLog(@"%@",self.drawingView.pathArray);
      NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:self.drawingView];
    [arrayData writeToFile:[DOCUMENTPATH stringByAppendingPathComponent:@"1.plist"] atomically:YES];
}

控制台O / P

 (
    "<ACEDrawingPenTool: 0x8b48d80;>",
    "<ACEDrawingPenTool: 0xa225530;>"
)

现在我用这个

编码数组
- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.pathArray forKey:@"array"];
}

此后我尝试获取对象

 - (id)initWithCoder:(NSCoder *)aDecoder
{
        self = [super initWithCoder:aDecoder];

    if (self) {
        self.pathArray = [aDecoder decodeObjectForKey:@"array"];
        NSLog(@"%@",self.pathArray);  // returns nil
        [self configure];
    }
    return self;
}

decodeObjectForKey返回nil

0 个答案:

没有答案