Alamofire无法正常工作-元组问题元素数量不同 [英] Alamofire Not Working Properly - Tuple Issue Different number of elements

查看:75
本文介绍了Alamofire无法正常工作-元组问题元素数量不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode 7.1

Using Xcode 7.1

在Alamofire responseJSON请求中,我无法放置4个参数。
下面是代码

In the Alamofire responseJSON request I cannot put 4 parameters. Below is the code

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

我收到此错误:元组类型为((NSURLRequest ?, NSHTTPURLResponse ?, Result)'(aka'(Optional,Optional,Result)')和'((_,_,_ ,_)'具有不同数量的元素(3个与4个)

I get this error: Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result)' (aka '(Optional, Optional, Result)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)

如果我从中删除了 错误参数responseJSON 并运行它...该应用程序已构建,但控制台上未打印json。.

If I remove the "error" parameter from responseJSON and run it...the app builds but no json is printed on the console..

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

控制台输出

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

There is no JSON being printed. If you go to sample URL from the code you will see JSON.

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

I have followed the instructions from GitHub but its not working

推荐答案

Alamofire v1.x具有 responseJSON 闭包的四个参数。 Alamofire v2.x具有三个参数。 Alamofire v3.x现在使用单个参数调用 Response

Alamofire v1.x had four parameters to the responseJSON closure. Alamofire v2.x had three parameters. Alamofire v3.x now calls the closure with a single parameter, a 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)
    }
}

或者,您可以使用 isSuccess isFailure 数据错误结果计算的属性,例如:

Alternatively, you can use the isSuccess, isFailure, value, data and error computed properties for Result, e.g.:

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

[此版本已针对Alamofire 3语法进行了更新。如果您需要Alamofire 2语法,请参考此问题的修订历史记录。]

[This has been updated for the Alamofire 3 syntax. If you need the Alamofire 2 syntax, please refer to this question's revision history.]

这篇关于Alamofire无法正常工作-元组问题元素数量不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆