Unarchiving data with new NSKeyedUnarchiver APIs while failed to satisfied unarchivedObjectOfClass it required

601 Views Asked by At

Firstly, I wanted to thank every of you have read the message and giving me feedback in any form.

Here it's what I'm trying to do. To replace deprecated API [NSKeyedUnarchiver unarchiveObjectWithData:myProfileData]; in objective-c to iOS 12 new API unarchivedObject<DecodedObjectType>(ofClass: DecodedObjectType.Type, from: Data) -> DecodedObjectType?

I unarchived data with old NSKeyedUnarchived APIs, printed it out and here is what I get:

assets =     {
        "albums" =         {
            "more_Album" = 0;
            photos =             (
            );
        };
    "shared_connections" = "<null>";
}

My first attempt was the blow, because I knew I archived data as dictionary, so I must unarchive it with the same class type:

NSError *error = nil;
NSDictionary *dictionary = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] fromData:myData error:&error];

However the error message showed:

Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSNull (0x11c39fe80).
Allowed classes are '{(
    "NSDictionary (0x11c39fa48)
)}

I thought maybe it's because I didn't include [NSNull class] in unarchivedObjectOfClass which caused the failure. I continued to make a set of unarchivedObject of classes in order to satisfy the complain:

NSError *error = nil;
NSSet *classSet = [NSSet setWithObjects:[NSDictionary class], [NSNull class], nil];
NSDictionary *dictionary = [NSKeyedUnarchiver unarchivedObjectOfClasses:classSet fromData:myData error:&error];

Unfortunately, it failed again.

Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSArray (0x11e79f778)

 Allowed classes are '{(
    "NSDictionary (0x11e79fa48) ,
    "NSNull (0x11e79fe80) )}'

I am not sure what is the right way to handle those Null value in dictionary while using new NSKeyedUnarchiver APIs.

1

There are 1 best solutions below

0
ThisIsJoe On

I got this all wrong and I realized I didn't include NSArray class in the NSSet and this is where complain came from.