Swift 5:'self'调用前使用'self.init'



我正在尝试使用所需的方便的可失败初始化器。这是我正在使用的代码:

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的其他一些相关问题。即这些:

  1. iOS Swift 便利初始化项 self 在 self.init 之前使用 self.init 调用
  2. 在自定义类上使用 NSCoding 时在 self.init 调用错误之前使用了"self">

所以我相应地安排了我的便利 init,如果有任何问题,请尝试返回 nil:

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

相关内容

最新更新