简体   繁体   English

在展开Optional值时意外发现nil - 使用ALAMOFIRE

[英]unexpectedly found nil while unwrapping an Optional value - Using ALAMOFIRE

I am trying to use Alamofire for GETs in JSON. 我试图在JSON中使用Alamofire进行GET。 When I use one URL - It works fine and when I use another I get an error unwrapping an optional value. 当我使用一个URL - 它工作正常,当我使用另一个时,我得到一个错误解开一个可选值。 I cannot seem to track where the error is coming from. 我似乎无法追踪错误的来源。 I have resorted in putting the code in ViewDidLoad to track the error. 我已经将代码放在ViewDidLoad中来跟踪错误。 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 : 但我知道它不是println的cos当我//他们 - 它仍然出现错误,继承代码:

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)
    }

but I use : 但我用:

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

it works fine and returns nil for the error. 它工作正常并返回错误的nil。

Any help would be great, as its driving me nuts. 任何帮助都会很棒,因为它让我疯狂。

The reason is simple: your URL has special characters. 原因很简单:您的URL有特殊字符。 So, if you do 所以,如果你这样做

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

It will return nil. 它将返回零。 You would need to format your URL to be suitable for making requests. 您需要将URL格式化为适合发出请求。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM