本文介绍了展开可选值时意外发现nil-使用ALAMOFIRE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Alamofire用于JSON中的GET。
当我使用一个URL时-它可以正常工作,而当我使用另一个URL时,我遇到了展开可选项值的错误。我似乎无法跟踪错误的来源。我已将代码放入ViewDidLoad中以跟踪错误。我不知道它是否是收件人,他们想要某种授权。但是我知道当我//它们时,它不是println的cos-它仍然会作为错误出现,这里是代码:

I am trying to use Alamofire for GETs in JSON.When I use one URL - It works fine and when I use another I get an error unwrapping an optional value. I cannot seem to track where the error is coming from. I have resorted in putting the code in ViewDidLoad to track the error. I don't know if its the recipient and they want some sort of authorisation. but I know its not the println's cos when i // them - it still comes up as an error , heres the code :

request(.GET, "https://api.doingdata.net/sms/send?api_service_key='APIKey'&msg_senderid=Edify-FYI&msg_to=&msg_text={otp|Edify-FYI|3600|ETEN|4} &msg_clientref=abcdef123456&msg_dr=0&output=json")
        .validate()
        .responseJSON { (_, _, _, error) in
            println(error)
    }

但我使用的是:

request(.GET, "http://httpbin.org/")
            .validate()
            .responseJSON { (_, _, _, error) in
                println(error)
        }

它可以正常工作并返回错误为零。

it works fine and returns nil for the error.

任何帮助都会很棒,因为它会让我发疯。

Any help would be great, as its driving me nuts.

推荐答案

原因很简单:您的URL有特殊字符。因此,如果您这样做

The reason is simple: your URL has special characters. So, if you do

let url = NSURL(string:yourURLString) //returns nil

它将返回nil。您需要格式化您的URL以适合发出请求。这是为您提供的一种解决方案。

It will return nil. You would need to format your URL to be suitable for making requests. Here is one solution for you.

var urlString = "https://api.doingdata.net/sms/send?api_service_key='APIKey'&msg_senderid=Edify-FYI&msg_to=&msg_text={otp|Edify-FYI|3600|ETEN|4} &msg_clientref=abcdef123456&msg_dr=0&output=json"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
request(.GET, urlString, parameters: nil, encoding: .JSON)

这篇关于展开可选值时意外发现nil-使用ALAMOFIRE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:54