I am developing an application that json parse. I'm using the AlertView for json messages. But I can not access the jsonmessage variable in the AlertView. if I put the AlertView in DO I get this error: "libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)" Sorry for my bad English. This is my code:
request.httpBody = postParameters.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with:request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
do {
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = myJSON {
var jsonmessage : String!
jsonmessage = parseJSON["message"] as! String?
print(jsonmessage)
}
} catch {
}
}
task.resume()
let alert = UIAlertController(title: "Alert", message: jsonmessage /*not accessible*/ , preferredStyle: .alert)
alert.addAction(UIAlertAction(title:"Ok", style:UIAlertActionStyle.default, handler:{ (UIAlertAction) in
_ = self.navigationController?.popToRootViewController(animated: true)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}))
self.present(alert, animated: true, completion: nil)