Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unexpectedly found nil while unwrapping an Optional value - Using ALAMOFIRE

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

but I use :

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.

like image 940
Jason Avatar asked Nov 28 '22 20:11

Jason


1 Answers

The reason is simple: your URL has special characters. 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. 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)
like image 92
avismara Avatar answered Dec 21 '22 05:12

avismara