Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initForReadingWithData is deprecated - initForReadingFromData returns nil

I have the following warning (Xcode 10.1 - iOS 12.1)

'initForReadingWithData:' is deprecated: first deprecated in iOS 12.0 - Use -initForReadingFromData:error: instead*

When I'm change the method to initForReadingFromData, the NSKeyedUnarchiver returns nil.

// Current code which produces the warning (but works fine) : 

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

NSMutableArray *loadedCredentialIdentities = (NSMutableArray *)[unarchiver decodeObjectForKey:kStoredCredentialIdentities];

[unarchiver finishDecoding];

...

// using initForReadingFromData produces no warning (but doesn't work - loadedCredentialIdentities is nil) : 


NSError *error = nil;
NSKeyedUnarchiver *unarchiver = unarchiver = [[NSKeyedUnarchiver    alloc] initForReadingFromData:data error:&error];

NSMutableArray *loadedCredentialIdentities = (NSMutableArray *)[unarchiver decodeObjectForKey:kStoredCredentialIdentities];

[unarchiver finishDecoding];
like image 713
Frank Möller Avatar asked Jun 14 '26 02:06

Frank Möller


2 Answers

Turning off secureCoding solved the problem.

[[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; 

unarchiver.requiresSecureCoding = NO;

[unarchiver decodeObjectForKey:...] 

In cases you don't have to be backward compatible it's better not to turn off secureCoding

like image 152
Frank Möller Avatar answered Jun 15 '26 23:06

Frank Möller


It because you are using decodeObjectForKey. If you don't set requiresSecureCoding to NO, you must use decodeObjectOfClass:forKey: instead.

like image 45
Thomas A. Avatar answered Jun 15 '26 22:06

Thomas A.