[NSKeyedUnarchiver initForReadingWithData:]:难以理解的存档(0xffffffff,0xffffffd8,0xffffffff,0xffffffe0,0x0,0x10,0x4a,0x46)

时间:2014-04-07 12:56:59

标签: ios nsdata nskeyedarchiver nskeyedunarchiver

在我的应用程序中,我将myObject存档到NSData,然后解压缩NSData以获取myObject。 崩溃痕迹

0   CoreFoundation                  0x2fe9ef46 __exceptionPreprocess + 126
1   libobjc.A.dylib                 0x3a1b36aa objc_exception_throw + 34
2   CoreFoundation                  0x2fe9ee88 +[NSException raise:format:] + 100
3   Foundation                      0x30815098 -[NSKeyedUnarchiver initForReadingWithData:] + 2464
4   Foundation                      0x30814680 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 48

MyObject包含以下属性:

@property (nonatomic,strong) NSData * imageData;
@property (nonatomic,strong) NSData * thumbnailData;
@property (nonatomic,strong) NSString * imagePath;
@property (nonatomic,strong) NSString * thumnailPath;
@property (nonatomic,strong) NSString * documentIdentifier;
@property (nonatomic,strong) NSString *paperIdentifier;

当尝试Unarchive NSData时,发生了这次崩溃。

NSData ==> OS_dispatch_data(138693字节)。

我使用[NSKeyedUnarchiver unarchiveObjectWithData:documentData]取消归档,并在myobject类中实现这些方法:

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:self.imageData forKey:IMAGE_DATA_KEY];
    [coder encodeObject:self.thumbnailData forKey:THUMBNAIL_DATA_KEY];
    [coder encodeObject:self.imagePath forKey:IMAGE_PATH_KEY];
    [coder encodeObject:self.thumnailPath forKey:THUMNBAIL_PATH_KEY];
    [coder encodeObject:self.paperIdentifier forKey:PAPER_ID_KEY];
    [coder encodeObject:self.documentIdentifier forKey:DOCUMENT_ID_KEY];
}

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        self.imageData = [coder decodeObjectForKey:IMAGE_DATA_KEY];
        self.thumbnailData = [coder decodeObjectForKey:THUMBNAIL_DATA_KEY];
        self.imagePath = [coder decodeObjectForKey:IMAGE_PATH_KEY];
        self.thumnailPath = [coder decodeObjectForKey:THUMNBAIL_PATH_KEY];
        self.paperIdentifier = [coder decodeObjectForKey:PAPER_ID_KEY];
        self.documentIdentifier = [coder decodeObjectForKey:DOCUMENT_ID_KEY];

    }
    return self;
}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

要设置和获取自定义对象,请使用这些...

+ (void)saveCustomObject:(YourClass *)obj {
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myEncodedObject forKey:@"key"];
}

+ (YourClass *)loadCustomObjectWithKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [defaults objectForKey:key];
YourClass *obj = (YourClass *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
return obj;
}