5

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

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

我在线路上得到错误'self' used before 'self.init' call,并且public required...在线路上再次出现同样的错误try? self.init(...

我在 Stack Overflow 上查看了其他一些相关问题。即这些:

  1. 在调用 self.init 之前使用的 iOS Swift 便利初始化器 self
  2. 在自定义类上使用 NSCoding 时,在 self.init 调用错误之前使用了“self”

所以我相应地安排了我的便利初始化,如果有任何问题,我会尝试返回 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 的错误。我怎样才能摆脱这个错误?

4

2 回答 2

3

一个解决方案是不调用指定的初始化器

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
}
于 2019-04-10T15:52:28.867 回答
0

这是我刚刚发现的另一个解决方案,它也有效。因为初始化器不抛出,所以最终代码可以是这样的:

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)
}
于 2019-04-12T15:26:14.577 回答