2

This is the part of my code that crashes:

let bodyData = "username=" + username + "&password=" + password

let URL: NSURL = NSURL(string: "URL OF A PHP FILE")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) // Crashes here !
{
    (response, data, error) in

    var output = NSString(data: data, encoding: NSUTF8StringEncoding)

    [Rest of the code]
}

the error is:

fatal error: unexpectedly found nil while unwrapping an Optional value

I thinks it's NSURLConnection.sendAsynchronousRequest that makes my app crashes cause i've debugged my app and it crashes there

This is the value of request:

<NSMutableURLRequest: 0x14d55b70> { URL: [URL OF PHP FILE] }

And i know that the data variable is not nil

Clément Bisaillon
  • 5,037
  • 8
  • 32
  • 53

2 Answers2

1

According to the sendAsynchronousRequest documentation, the response, data, and error variables in the callback are all implicitly unwrapped optionals. If any of them is nil, a fatal error will be thrown at runtime.

To fix this, define them as normal optionals in the callback block and unwrap them as usual using optional binding or value checking.

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
    (response: NSURLResponse?, data: NSData?, error: NSError?) in
    // ...
}
nickgraef
  • 2,397
  • 2
  • 22
  • 26
  • I think the document is wrong since data and error simply cannot be non-nil at the same time, otherwise it doesn't make any sense. – superarts.org Apr 27 '15 at 04:40
  • The documentation is correct in that it describes the behavior of the function. This seems more like a flaw in the design of the Swift API for `NSURLConnection` when it was ported from Objective-C. – nickgraef Apr 27 '15 at 16:16
  • Right. In the document it's said `handler: (NSURLResponse!, NSData!, NSError!) -> Void)`, which should be `handler: (NSURLResponse?, NSData?, NSError?) -> Void)`, and literally the document is right. I think part of the document is exported and it correctly reflects the `Swift` implementation, and the implementation is wrong. – superarts.org Apr 28 '15 at 00:11
0

The request is working fine, but I suspect the response is not because of invalid formatted "URL OF A PHP FILE". The code that process this data is the one crashing.

I can generate the same error (I tested the code on AppDelegate) when data is nil and invalid URL by encoding data

let dataStr = NSString(data: data, encoding: NSUTF8StringEncoding)

If there is safety check beforehand, it is not crashing though:

if let notNilData = data {
    let dataStr = NSString(data: notNilData, encoding: NSUTF8StringEncoding)
} else {
    print("nil data")
}
Peter
  • 1,109
  • 7
  • 5
  • I tried but it is crashing before printing "nil data" and i did a curl with all the post parameters i need and it return all the data that i need so i don't think that the data is nil – Clément Bisaillon Nov 03 '14 at 02:32
  • Then, I am out of idea here. Sorry. The second suspect I have is threading issue. Maybe you can try to execute that code block somewhere else. – Peter Nov 03 '14 at 03:01