Alamofire 无法正常工作 - 元组问题不同数量的元素

Alamofire Not Working Properly - Tuple Issue Different number of elements

使用 Xcode 7.1

在 Alamofire 响应JSON 请求中我不能输入 4 个参数。 下面是代码

 let url2 = "https://httpbin.org/get"
    Alamofire.request(.GET, url2).responseJSON{ request, response, JSON, error  in
        print(JSON)
    }

我收到此错误:元组类型“(NSURLRequest?, NSHTTPURLResponse?, Result)”(又名“(Optional, Optional, Result)”)和“(_, _, _, _)”有不同元素数量(3 对 4)

如果我从 responseJSON 和 运行 中删除“error”参数...应用程序构建但没有 json 打印在控制台上..

  let url2 = "https://httpbin.org/get"
    Alamofire.request(.GET, url2).responseJSON{ request, response, JSON in
        print(JSON)
    }

控制台输出

没有 JSON 正在打印。如果您从代码中转到示例 URL,您将看到 JSON.

我已按照 GitHub 的说明进行操作,但它不起作用

Alamofire v1.x 有四个参数到 responseJSON 闭包。 Alamofire v2.x 有三个参数。 Alamofire v3.x 现在用一个参数调用闭包,一个 Response:

Alamofire.request(.GET, url2).responseJSON { response in
    switch (response.result) {
    case .Success(let value):
        print(value)
    case .Failure(let error):
        if let data = response.data, let string = String(data: data, encoding: NSUTF8StringEncoding) {
            print(string)
        }
        print(error)
    }
}

或者,您可以为 Result 使用 isSuccessisFailurevaluedataerror 计算属性,例如:

Alamofire.request(.GET, url2).responseJSON { response in
    print(response.result.value)
}

[这已经针对 Alamofire 3 语法进行了更新。如果需要Alamofire 2语法,请参考本题的revision history.]

Github 上的文档尚未更新到最新版本的 Alamofire。

要查看 Rob 指出的属性,请查看框架的源代码。