Davis Hoo
Davis Hoo

Reputation: 31

"The data couldn’t be written because it isn’t in the correct format" occurs while archiving a custom class with NSKeyedArchiver

I was trying a new API in iOS12:

[NSKeyedArchiver archivedDataWithRootObject:<#(nonnull id)#> requiringSecureCoding:<#(BOOL)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>]

What I was trying to do is very simple, archive a custom class, here is the code:

A class named Cat:

@interface Cat : NSObject <NSCoding>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;

+ (void)saveThisCat:(Cat *)cat; 
+ (Cat *)getThisCat;

@end


@implementation Cat

- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}

+ (void)saveThisCat:(Cat *)cat {
    NSError *error = nil;
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *filePath = [docPath stringByAppendingPathComponent:@"cat.plist"];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cat requiringSecureCoding:YES error:&error];
    // ***Error occurs here!!!***
    NSLog(@"=== Error Info: %@ ===", [error localizedDescription]);
    [data writeToFile:filePath atomically:YES];
}

+ (Cat *)getThisCat {
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *filePath = [docPath stringByAppendingPathComponent:@"cat.plist"];
    Cat *retVal = [NSKeyedUnarchiver unarchivedObjectOfClass:[Cat class] fromData:[NSData dataWithContentsOfFile:filePath] error:nil];
    return retVal;
}

@end

Usage:

Cat *totoro = [Cat new];
totoro.name = @"totoro";
totoro.age = 1;

NSLog(@"=== The cat's name is %@", totoro.name);
NSLog(@"=== The cat's age is %d", totoro.age);

[Cat saveThisCat:totoro];

Cat *resultCat = [Cat getThisCat];

NSLog(@"=== The cat's name is %@", resultCat.name);
NSLog(@"=== The cat's age is %d", resultCat.age);

And the error info (Generated by using archivedDataWithRootObject while executing saveThisCat method)

=== Error Info: The data couldn’t be written because it isn’t in the correct format. ===

Is there anything wrong? Please point it out, thanks a lot!

Upvotes: 3

Views: 3407

Answers (2)

Rob
Rob

Reputation: 438487

When you call unarchivedObjectOfClass, use the error parameter and it will supply an NSError:

Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'Cat' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'Cat' does not adopt it.}

As vadian said, adopt NSSecureCoding.

Whenever you have a failure, always avail yourself of available NSError parameters.

Upvotes: 4

vadian
vadian

Reputation: 285290

You have to adopt NSSecureCoding

@interface Cat : NSObject <NSSecureCoding>

and in the implementation add the required class property

+ (BOOL)supportsSecureCoding {
    return YES;
}

Upvotes: 7

Related Questions