Reputation: 665
I'm trying to unarchive a saved object from NSKeyedUnarchiver. The unarchived data is ok, but I'm not able to cast it from Any? type to it's actual type (OIDAuthState).
public func loadState() {
if let archivedAuthState = UserDefaults.standard.object(forKey: stateKeychainIdentifier) as? Data {
if let authState = NSKeyedUnarchiver.unarchiveObject(with: archivedAuthState) {
setAuthState(authState as! OIDAuthState)
}
}
}
I've also tried
if let authState: OIDAuthState = ....
if let authState = NSKeyedUnarchiver.unarchiveObject(with: archivedAuthState) as? OIDAuthstate { ...
which all fail. OIDAuthState is an Objective-C class bridged to Swift, and it inherits from NSObject and NSSecureCoding.
Upvotes: 1
Views: 150
Reputation: 665
The fix for this is :
NSKeyedUnarchiver.setClass(OIDAuthState.self, forClassName: "OIDAuthState")
StackOverflow answer reference
Upvotes: 4