2

在我的应用程序中,我将 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;
}

任何人都可以帮助我吗?

4

1 回答 1

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;
}
于 2014-04-07T13:06:49.027 回答