我有一个类,它必须是OIDAuthState的子类(这个类是NSObject,符合NSSecureCoding(,
编码类OIDAuthState的代码在之前运行良好
self.data = try NSKeyedArchiver.archivedData(withRootObject: authState, requiringSecureCoding: true)
一旦我创建了这个包含的子类,并将一个简单的布尔值添加到类isExpiredTokenEnforced
中,我现在在使用NSKeyedArchiver、时会收到错误
错误显示:
NSDebugDescription=存档期间捕获异常:***-decodeBoolForKey:仅为抽象类定义。定义-[NSKeyedArchiver decodeBoolForKey:]!
这是我试图归档的子类,下面是重写方法来编码和解码这个类,我只是对这个1额外的属性进行编码和解码,然后将编码器传递给父类
class AuthenticationStateManager: OIDAuthState, AuthenticationState {
var isExpiredTokenEnforced = false
var lastTokenResponseInterface: TokenResponse? {
if isExpiredTokenEnforced {
return EnforcedExpiredTokenResponse(idToken: super.lastTokenResponse?.idToken,
accessToken: super.lastTokenResponse?.accessToken)
} else {
return super.lastTokenResponse
}
}
override static var supportsSecureCoding: Bool { true }
required override init(authorizationResponse: OIDAuthorizationResponse?, tokenResponse: OIDTokenResponse?, registrationResponse: OIDRegistrationResponse?) {
super.init(authorizationResponse: authorizationResponse, tokenResponse: tokenResponse, registrationResponse: registrationResponse)
}
required init?(coder: NSCoder) {
coder.encode(isExpiredTokenEnforced, forKey: "isExpiredTokenEnforced")
super.init(coder: coder)
}
override func encode(with coder: NSCoder) {
self.isExpiredTokenEnforced = coder.decodeBool(forKey: "isExpiredTokenEnforced")
super.encode(with: coder)
}
}
我不明白为什么会失败我找不到相关信息来解决这个问题,
有人知道这里可能出了什么问题吗?
提前感谢您抽出时间。
您混淆了编码器和解码器的功能。您的func encode()
正在尝试调用decodeBool。它应该调用encode()
。
类似地,init?(coder:)
函数应该调用decode(或者更确切地说是decodeBool(forKey:)
(