【发布时间】:2018-12-05 21:29:03
【问题描述】:
我正在使用以下函数将图像上传到给定的 url。 我根据对这些问题的回答构建了这个函数: NSURLConnection Using iOS Swift 和 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)")
}
但是,当我运行我的应用程序时,我总是在以下行收到“致命错误:在展开可选值时意外发现 nil”:
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error:nil)!
我做错了什么?谢谢
【问题讨论】:
标签: ios swift nsurlconnection