0

我正在学习使用 Locksmith 框架存储令牌的 OAuth 教程。本教程是用旧版本的 Swift 和旧版本的 Locksmith 框架编写的。在 Swift 4 中重新创建示例时,我注意到 Locksmith 4.0 使用带有 do try catch 块的错误处理。我正在尝试将教程的可选绑定转换为使用框架的错误处理方法。我第一次尝试自己,但我无法理解为什么教程使用可选绑定的心理联系。

这里是教程的摘录:

var OAuthToken: String?
{
  set
  {
    if let valueToSave = newValue
    {
      let error = Locksmith.saveData(["token": valueToSave], forUserAccount: "github")
      if let errorReceived = error
      {
        Locksmith.deleteDataForUserAccount("github")
      }
      addSessionHeader("Authorization", value: "token \(newValue)")      
    }
    else // they set it to nil, so delete it
    {
      Locksmith.deleteDataForUserAccount("github")
      removeSessionHeaderIfExists("Authorization")
    }
  }
  get
  {
    // try to load from keychain
    let (dictionary, error) = Locksmith.loadDataForUserAccount("github")
    if let token =  dictionary?["token"] as? String {
      return token
    }
    removeSessionHeaderIfExists("Authorization")
    return nil
  }
}

这就是我所拥有的。我相信我在滥用与可选绑定相关的 catch 语句:

var OAuthTokenCompletionHandler: ((NSError?) -> Void)?

    var authToken: String? {
        set {
            if let valueToSave = newValue{
                do{
                try Locksmith.saveData(data: ["token" : valueToSave], forUserAccount: "AzureMediaServices")
                } catch {
                    //could not save the data into keychain
                    //handle the error somehow
                    try Locksmith.deleteDataForUserAccount(userAccount: "AzureMediaServices")
                }

                addSessionHeader("Authorization", value: "Bearer \(valueToSave)")
            } else {
                //try to set it to nil
                removeSessionHeaderIfExists("Authorization")
            }
        }
        get {
            //TODO: implement
        }
    }
4

2 回答 2

0

我认为这里的主要问题是原始代码没有处理Locksmith.deleteDataForUserAccount. Locksmith.deleteDataForUserAccount被调用两次,这个计算属性变得相当复杂。如果这两个函数调用的错误处理相同,我建议将其提取到辅助函数中。

这是我的第一次尝试:

var OAuthTokenCompletionHandler: ((NSError?) -> Void)?

var authToken: String? {
    set {
        guard let valueToSave = newValue else {
            do {
                try Locksmith.deleteDataForUserAccount(userAccount: "AzureMediaServices")
            }
            catch {
                // handle the error somehow
            }

            removeSessionHeaderIfExists("Authorization")
            return
        }

        do{
            try Locksmith.saveData(data: ["token" : valueToSave], forUserAccount: "AzureMediaServices")
        } catch {
            do {
                try Locksmith.deleteDataForUserAccount(userAccount: "AzureMediaServices")
            }
            catch {
                // handle the error somehow
            }
        }

        addSessionHeader("Authorization", value: "Bearer \(valueToSave)")
    }
    get {
        guard let token = Locksmith.loadDataForUserAccount("github")?["token"] as? String {
            removeSessionHeaderIfExists("Authorization")
            return nil
        }
        return token
    }
}
于 2018-07-02T20:30:33.513 回答
0
var OAuthToken: String? {
    set {
        guard let newValue = newValue else {
            let _ = try? Locksmith.deleteDataForUserAccount(userAccount: "github")
            return
        }
        guard let _ = try? Locksmith.updateData(data: ["token": newValue],
                                                forUserAccount: "github") else {
                                                    let _ = try? Locksmith.deleteDataForUserAccount(userAccount: "github")
                                                    return
        }
    }
    get {
        // try to load from keychain
        let dictionary = Locksmith.loadDataForUserAccount(userAccount: "github")
        return dictionary?["token"] as? String
    }
}
于 2018-07-28T02:05:06.427 回答