3

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!

Davis Hoo
  • 31
  • 1
  • 3

2 Answers2

7

You have to adopt NSSecureCoding

@interface Cat : NSObject <NSSecureCoding>

and in the implementation add the required class property

+ (BOOL)supportsSecureCoding {
    return YES;
}
vadian
  • 274,689
  • 30
  • 353
  • 361
2

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Underrated answer. I was printing the localized error, but turns out the full Error object was pinpointing exactly what's wrong – Morten J Jan 10 '22 at 13:39