khanh.tran.vinh
khanh.tran.vinh

Reputation: 665

Casting fails from unarchived data from NSKeyedUnarchiver, from Any? type to OIDAuthState

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)
            }
        }
    }

enter image description here

enter image description here

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

Answers (1)

khanh.tran.vinh
khanh.tran.vinh

Reputation: 665

The fix for this is :

NSKeyedUnarchiver.setClass(OIDAuthState.self, forClassName: "OIDAuthState")

StackOverflow answer reference

Upvotes: 4

Related Questions