2

In my app i Archive myObject to NSData and then Unarchive NSData to get myObject. Crash trace

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 contains these Properties:

@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;

When try to Unarchive NSData this crash occurred.

NSData ==> OS_dispatch_data (138693 bytes).

I use [NSKeyedUnarchiver unarchiveObjectWithData:documentData] to unarchive and in myobject class implement these methods:

- (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;
}

Anyone can help me please ?

1 Answers1

0

To set and fetch the custom objects use these...

+ (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;
}
borncrazy
  • 1,589
  • 1
  • 12
  • 9