Swift 5: 'self' 在 'self.init' 调用之前使用
Swift 5: 'self' used before 'self.init' call
我正在尝试使用所需的便捷可失败初始化程序。这是我正在使用的代码:
public init(authState: OIDAuthState, config: [String: String], accessibility: CFString = kSecAttrAccessibleWhenUnlockedThisDeviceOnly) throws {
self.authState = authState
self.config = config
self.accessibility = accessibility
super.init()
KeycloakAuth.configuration = config
}
public required convenience init?(coder aDecoder: NSCoder) {
try? self.init(authState: aDecoder.decodeObject(forKey: "authState") as! OIDAuthState,
config: aDecoder.decodeObject(forKey: "config") as! [String: String],
accessibility: aDecoder.decodeObject(forKey: "accessibility") as! CFString)
}
我在 public required...
行收到错误 'self' used before 'self.init' call
,在 try? self.init(...
行又收到同样的错误。
我查看了 Stack Overflow 上的其他一些相关问题。即这些:
所以我相应地安排了我的便利初始化来尝试 return 如果有任何问题则为零:
public required convenience init?(coder aDecoder: NSCoder) {
guard
let authState = aDecoder.decodeObject(forKey: "authState") as? OIDAuthState,
let config = aDecoder.decodeObject(forKey: "config") as? [String: String]
else {
print("KeycloakTokenManager: There was an error intializing authState or config")
return nil
}
let accessibility = aDecoder.decodeObject(forKey: "accessibility") as! CFString
try? self.init(authState: authState, config: config, accessibility: accessibility)
}
但我在同一代码(初始化程序和调用 self.init
)上遇到了相同的错误。有趣的是,我的项目在 Swift 4 上构建良好,但我没有听说这是一个错误 Swift 5。我怎样才能摆脱这个错误?
一个解决方案是不调用指定的初始化器
public required init?(coder aDecoder: NSCoder) {
guard
let authState = aDecoder.decodeObject(forKey: "authState") as? OIDAuthState,
let config = aDecoder.decodeObject(forKey: "config") as? [String: String]
else {
print("KeycloakTokenManager: There was an error intializing authState or config")
return nil
}
self.authState = authState
self.config = config
self.accessibility = aDecoder.decodeObject(forKey: "accessibility") as! CFString
super.init()
KeycloakAuth.configuration = config
}
这是我刚刚发现的另一种解决方案,它也有效。因为初始化不抛出,所以最终的代码可以是这样的:
public init(authState: OIDAuthState, config: [String: String], accessibility: CFString = kSecAttrAccessibleWhenUnlockedThisDeviceOnly) {
self.authState = authState
self.config = config
self.accessibility = accessibility
super.init()
KeycloakAuth.configuration = config
}
public required convenience init?(coder aDecoder: NSCoder) {
self.init(authState: aDecoder.decodeObject(forKey: "authState") as! OIDAuthState,
config: aDecoder.decodeObject(forKey: "config") as! [String: String],
accessibility: aDecoder.decodeObject(forKey: "accessibility") as! CFString)
}
我正在尝试使用所需的便捷可失败初始化程序。这是我正在使用的代码:
public init(authState: OIDAuthState, config: [String: String], accessibility: CFString = kSecAttrAccessibleWhenUnlockedThisDeviceOnly) throws {
self.authState = authState
self.config = config
self.accessibility = accessibility
super.init()
KeycloakAuth.configuration = config
}
public required convenience init?(coder aDecoder: NSCoder) {
try? self.init(authState: aDecoder.decodeObject(forKey: "authState") as! OIDAuthState,
config: aDecoder.decodeObject(forKey: "config") as! [String: String],
accessibility: aDecoder.decodeObject(forKey: "accessibility") as! CFString)
}
我在 public required...
行收到错误 'self' used before 'self.init' call
,在 try? self.init(...
行又收到同样的错误。
我查看了 Stack Overflow 上的其他一些相关问题。即这些:
所以我相应地安排了我的便利初始化来尝试 return 如果有任何问题则为零:
public required convenience init?(coder aDecoder: NSCoder) {
guard
let authState = aDecoder.decodeObject(forKey: "authState") as? OIDAuthState,
let config = aDecoder.decodeObject(forKey: "config") as? [String: String]
else {
print("KeycloakTokenManager: There was an error intializing authState or config")
return nil
}
let accessibility = aDecoder.decodeObject(forKey: "accessibility") as! CFString
try? self.init(authState: authState, config: config, accessibility: accessibility)
}
但我在同一代码(初始化程序和调用 self.init
)上遇到了相同的错误。有趣的是,我的项目在 Swift 4 上构建良好,但我没有听说这是一个错误 Swift 5。我怎样才能摆脱这个错误?
一个解决方案是不调用指定的初始化器
public required init?(coder aDecoder: NSCoder) {
guard
let authState = aDecoder.decodeObject(forKey: "authState") as? OIDAuthState,
let config = aDecoder.decodeObject(forKey: "config") as? [String: String]
else {
print("KeycloakTokenManager: There was an error intializing authState or config")
return nil
}
self.authState = authState
self.config = config
self.accessibility = aDecoder.decodeObject(forKey: "accessibility") as! CFString
super.init()
KeycloakAuth.configuration = config
}
这是我刚刚发现的另一种解决方案,它也有效。因为初始化不抛出,所以最终的代码可以是这样的:
public init(authState: OIDAuthState, config: [String: String], accessibility: CFString = kSecAttrAccessibleWhenUnlockedThisDeviceOnly) {
self.authState = authState
self.config = config
self.accessibility = accessibility
super.init()
KeycloakAuth.configuration = config
}
public required convenience init?(coder aDecoder: NSCoder) {
self.init(authState: aDecoder.decodeObject(forKey: "authState") as! OIDAuthState,
config: aDecoder.decodeObject(forKey: "config") as! [String: String],
accessibility: aDecoder.decodeObject(forKey: "accessibility") as! CFString)
}