I'm trying to test an OAuth2 implementation in a Xcode Playground, using a PHP API and a Swift client. Basically, my code looks like this
let url = NSURL(string: "http://localhost:9142/account/validate")!
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody!.setValue("password", forKey: "grant_type")
// Other values set to the HTTPBody
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
// Handle response here
}
But I keep getting this error when I instantiate the url variable :
fatal error: unexpectedly found nil while unwrapping an Optional value
I tried not unwrapping it when I instantiate it but rather when I use it, it didn't change anything, the error appears on the first time I unwrap it.
It keeps getting even weirder.. The following
let url = NSURL(string: "http://localhost:9142/account/validate")!
println(url)
outputs
http://localhost:9142/account/validate fatal error: unexpectedly found nil while unwrapping an Optional value
I really don't understand where the error can come from, as I'm really new to Swift
url = NSURL(string="http://test.com/something?argument=variable containing space")
– Kushal Ashok