How to resolve - unarchiveObjectWithData:' is deprecated: first deprecated in iOS 12.0

I am trying to replace the deprecated unarchiveObjectWithData API with unarchivedObjectOfClass.

My class looks something like this

Class B:<<NSSecureCoding>

{
+(BOOL)supportsSecureCoding
 { 
return YES;

  }

}
Class C <<NSSecureCoding>

{

 property of class B;

+ (BOOL)supportsSecureCoding
{ 
return YES; 

}

 }
Class A<<NSSecureCoding> 
{
property of class B property of class c

(BOOL)supportsSecureCoding
{
 return YES; 
}

}

Tried solutions:

Option 1:

Archive code

NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:(array of objects of type A) requiringSecureCoding:YES error:&error]; // Encrypting archiveData and writing to file

Unarchive code

// fetching data from file // decrypting it

NSSet *classesSet = [NSSet setWithObjects: [NSMutableArray classForCoder], [A classForCoder], [NSString classForCoder], [NSNumber classForCoder], [B classForCoder], [C classForCoder] , nil]; NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:decryptedData error:&error]; [unArchiver setRequiresSecureCoding:YES]; array of objects of Type A = [unArchiver decodeObjectOfClasses:classesSet forKey:NSKeyedArchiveRootObjectKey];

Issue:

Unable to unarchive all the properties. Properties have nil values including properties of type NSString, BOOL, custom class

Option: 2

Use +unarchivedObjectOfClass:fromData:error

Unarchive code:

NSSet *classesSet = [NSSet setWithObjects: [NSMutableArray classForCoder], [A classForCoder], [NSString classForCoder], [NSNumber classForCoder], [B classForCoder], [C classForCoder] , nil];

array of objects of Type A = [NSKeyedUnarchiver unarchivedObjectOfClasses:classesSet fromData:storeData error:&error]

Issue:

"UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'Class B'. Allowed classes are '{NSMutable Array}"

Can someone please assist with this issue. I have tried all the available solutions online. I couldn't find a documentation from Apple on how to consume the new API in a correct way.

Under option 1 you say you archive with:

[NSKeyedArchiver archivedDataWithRootObject:(array of objects of type A) requiringSecureCoding:YES error:&error]

And then unarchive with:

NSSet *classesSet = [NSSet setWithObjects: [NSMutableArray classForCoder], [A classForCoder], [NSString classForCoder], [NSNumber classForCoder], [B classForCoder], [C classForCoder] , nil]; 
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:decryptedData error:&error];
 [unArchiver setRequiresSecureCoding:YES]; 

unarchivedArray = [unArchiver decodeObjectOfClasses:classesSet forKey:NSKeyedArchiveRootObjectKey];

Which doesn't match. Why are you specifying all those classes (NSString, NSNumber, etc.) if the array only contains TypeA? If you archive a single array that only contains TypeA objects then why don't you you use -decodeArrayOfObjectsOfClass:forKey:

NSArray *unarchivedArray = [unArchiver decodeArrayOfObjectsOfClass:[TypeA class] forKey:NSKeyedArchiveRootObjectKey];

Thanks [@Macho Man ***** Savage](https://developer.apple.com/forums/profile/Macho Man ***** Savage) for the reply.

I have tried this option as well -

NSArray *unarchivedArray = [unArchiver decodeArrayOfObjectsOfClass:[TypeA class] forKey:NSKeyedArchiveRootObjectKey];

I got the same error.

"UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'Class B'. Allowed classes are '{NSMutable Array}" ** Which doesn't match. Why are you specifying all those classes (NSString, NSNumber, etc.) if the array only contains TypeA? **- As the above solution didn't work, thought of specifying all the Class properties types in the the classes set to check if it works.

How to resolve - unarchiveObjectWithData:' is deprecated: first deprecated in iOS 12.0
 
 
Q