I want to send some data to the server but when I try , I get this error
I think is a problem with dispatch maybe I am not using it correctly but I have no experience with xcode or swift, so I don't know how to understand well the log error window, any help is welcome, thaks for ur time
This is my code:
func updateAndDismiss6() {
SharingManager.sharedInstance.FinalDiag = self.Diag.text!
SharingManager.sharedInstance.TreatmentDays = self.Trata.text!
SharingManager.sharedInstance.SystematicTreat = self.TrataSu.text!
SharingManager.sharedInstance.LidoPatch = self.ParcheLido.text!
}
@IBAction func IrResumen(sender: AnyObject) {
updateAndDismiss6()
let prefs = NSUserDefaults.standardUserDefaults()
let userid = prefs.stringForKey("userid")! as String
let Inicial = SharingManager.sharedInstance.Initials
let Edad = SharingManager.sharedInstance.Age
let Resultados = SharingManager.sharedInstance.Result
let Diagnostico = SharingManager.sharedInstance.FinalDiag
let Ttratamiento = SharingManager.sharedInstance.TreatmentDays
let TsUsado = SharingManager.sharedInstance.SystematicTreat
let Plidocaina = SharingManager.sharedInstance.LidoPatch
if ( Inicial == "" || Edad == "" || Resultados == "" || Diagnostico == "" || Ttratamiento == "" || TsUsado == "" || Plidocaina == "" || userid == "") {
let alertView:UIAlertView = UIAlertView()
alertView.title = "No data"
alertView.message = "error de datos"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
else {
let url_servicio = "http://arasoftltda.com/grunenthal/app/muestra.php"
let request = NSMutableURLRequest(URL: NSURL(string: url_servicio)!)
request.HTTPMethod = "POST"
//let postString = "nombre=pruebafromios&[email protected]®istro=registro&esp=esp&pais=Colombia&ciudad=Cogua&contrasena=123"
let postString = "iniciales=\(Inicial)&edad=\(Edad)&resultado=\(Resultados)&diagnostico=\(Diagnostico)&dias=\(Ttratamiento)&tratamiento=\(TsUsado)&parche=\(Plidocaina)&id=\(userid)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error -> Void in
if (error != nil){
print(error!.localizedDescription)
} else{
let nsdata:NSData = NSData(data: data!)
do{
let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.MutableContainers)
let id = json["id"] as! String
if (id != ""){
print (id)
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.performSegueWithIdentifier("Resumen", sender: self)}
}
}
catch{
print("Error del JSON")
let alertView:UIAlertView = UIAlertView()
alertView.title = "Fallo al enviar datos!"
alertView.message = "Error de conexion con el servidor"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
}
}
task.resume()
}
}

unowned selfin this context. If the view controller really could be dismissed, you'd useweak self. Butunownedcan leave you with a dangling reference. Also, I'd advise against usingUIAlertView. Nowadays you'd useUIAlertController.postStringand make sure (a) that none of those variables were optionals that needed to be unwrapped; and (b) that there weren't any unreserved characters (e.g. punctuation, spaces, etc.). When you compose ax-www-form-urlencodedrequest, you generally would percent escape the values, just to be safe.