使用闭包将API数据保存到结构字段

时间:2019-01-02 13:53:18

标签: swift closures

我正在尝试将从API接收到的字符串保存到struct的String字段中。但是,我得到了:

  

关闭无法隐式捕获变异的自身参数

任何想法如何使这项工作?谢谢:)

active environment : base
active env location : C:\Users\***\AppData\Local\Continuum\anaconda3
   user config file : C:\Users\***\.condarc
populated config files : C:\Users\***\.condarc
      conda version : 4.5.4
conda-build version : 3.10.5
     python version : 3.6.5.final.0
   base environment : C:\Users\***\AppData\Local\Continuum\anaconda3  (writable)
       channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                      https://repo.anaconda.com/pkgs/main/noarch
                      https://repo.anaconda.com/pkgs/free/win-64
                      https://repo.anaconda.com/pkgs/free/noarch
                      https://repo.anaconda.com/pkgs/r/win-64
                      https://repo.anaconda.com/pkgs/r/noarch
                      https://repo.anaconda.com/pkgs/pro/win-64
                      https://repo.anaconda.com/pkgs/pro/noarch
                      https://repo.anaconda.com/pkgs/msys2/win-64
                      https://repo.anaconda.com/pkgs/msys2/noarch
      package cache : C:\Users\***\AppData\Local\Continuum\anaconda3\pkgs
                      C:\Users\***\AppData\Local\conda\conda\pkgs
   envs directories : C:\Users\***\AppData\Local\Continuum\anaconda3\envs
                      C:\Users\***\AppData\Local\conda\conda\envs
                      C:\Users\***\.conda\envs
           platform : win-64
         user-agent : conda/4.5.4 requests/2.18.4 CPython/3.6.5 Windows/10 Windows/10.0.17134
      administrator : False
         netrc file : None
       offline mode : False

我在这里打# conda environments: # base * C:\Users\***\Anaconda2 C:\Users\***\AppData\Local\Continuum\anaconda3

func getAPIWord(ID: Int, completion: @escaping(_ wordResult: String) - > Void) {
    let testURL = URL(string: "http://\(ipAddress)/api/v\(version)/words/" + String(ID))
    let task = URLSession.shared.dataTask(with: testURL!) {
        (data, response, error) in
            guard let data = data, error == nil
        else {
            print(error!);
            return
        }
        let decoder = JSONDecoder()
        let json_parsed =
        try !decoder.decode(Word.self, from: data)
        completion(json_parsed.word)
    }
    task.resume()
}

0 个答案:

没有答案