My app currently uses this deprecated function:
id unarchivedObject=[NSKeyedUnarchiver unarchiveObjectWithData:codedData];
if([unarchivedObject isKindOfClass:[NSDictionary class]]){
// currently returns TRUE when reading existing user data.
}
To update, I've converted to this:
id unarchivedObject=[NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] fromData:codedData error:nil];
if([unarchivedObject isKindOfClass:[NSDictionary class]]){
// currently returns FALSE when reading existing user data.
}
The data was originally encoded like this:
-(void)encodeWithCoder:(NSCoder*)encoder{
[encoder encodeObject:text forKey:@"text"];
}
-(instancetype)initWithCoder:(NSCoder*)decoder{
if(self=[super init]){
text=[decoder decodeObjectForKey:@"text"];
}
What could be causing the IF statement to return FALSE using the newer code?
Please note that I am concerned primarily with reading existing data stored prior to deprecating the Archiving functions. Simply changing to the newer functions does not resolve the issue.
Interesting question! I've been supporting iOS 10.0 so I haven't encountered such issue until I saw this. I was tinkering for an hour and I successfully found the issue.
It's because your
unarchivedObjectobject is nil!If you use the parameter
errorin the new method, you would see an error like this:But how do we get the correct value for this
unarchivedObjectand not nil? It would take a couple of steps.First off, make your model/class conform to
<NSCoding, NSSecureCoding>Example: QTPerson.h
And then implement the protocol methods:
QTPerson.m
And then when archiving an object, you would want to pass
YESto the parameterrequiringSecureCoding, like so:Lastly, when unarchiving, just do what you did correctly, like so:
Voila! You'll get nonnull object
unarchivedObject, hence the TRUE/YES value you're looking for!