I am using the following function to upload a image to a given url. I built this function based on the answers given to these to questions: NSURLConnection Using iOS Swift and How to send UIImage in JSON format, by filling a NSDictionary
func uploadFileToUrl(url:NSURL){
var request = NSMutableURLRequest(URL:url)
request.HTTPMethod = "POST"
request.HTTPBody = NSData.dataWithData(UIImagePNGRepresentation(image))
var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
var error: AutoreleasingUnsafeMutablePointer<NSErrorPointer?> = nil
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error:nil)!
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
if (error != nil) {
println("Request didn't go through")
}
println("Synchronous\(jsonResult)")
}
However when I run my app I always get an "fatal error: unexpectedly found nil while unwrapping an Optional value" on the following line:
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error:nil)!
What am I doing wrong? Thanks