We have multiple classes that conform to NSSecureCoding
protocol.
@interface ClassA : NSObject <NSSecureCoding>
// ...
@end
@interface ClassB : NSObject <NSSecureCoding>
// ...
@end
We notice that NSArray
also conforms to NSSecureCoding
. Therefore, we try the following.
For encoding:
NSArray* array = ...
[archiver encodeObject:array forKey:@"AirdropDataKey"];
For decoding
NSArray* array = [unarchiver decodeObjectOfClass:[NSArray class]
forKey:@"AirdropDataKey"];
And I get this following error message.
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'value for key 'NS.objects' was of unexpected class 'ClassA'. Allowed classes are '{(
NSArray
)}'.'
Anybody could explain why and whether it is possible to resolve this issue?
NSCoder
provides an additional method decodeObjectOfClasses:forKey:
, where a set of expected objects could be passed. This allows to decode nested structures.
Just pass a set with the NSArray
and your ClassA
and ClassB
class:
NSSet *classes = [NSSet setWithObjects:[NSArray class], [ClassA class] ,[ClassB class], nil];
NSArray* array = [unarchiver decodeObjectOfClasses:classes forKey:@"AirdropDataKey"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With