Alamofire无法正常工作

编程入门 行业动态 更新时间:2024-10-22 04:57:36
本文介绍了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 = "httpbin/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 = "httpbin/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无法正常工作

本文发布于:2023-11-26 02:57:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!