1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
khanh.tran.vinh
  • 655
  • 5
  • 10

1 Answers1

3

The fix for this is :

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

StackOverflow answer reference

khanh.tran.vinh
  • 655
  • 5
  • 10