Ниже приведен мой код (Swift 4.2.1) для получения ответа от API. Но при вызове он показывает некоторую ошибку как,
Невозможно присвоить значение типа 'NSDictionary?'набрать 'String?'
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
return
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
do {
let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary
print(jsonResponse!)
responsevalue = jsonResponse
let alert1 = UIAlertController(title: "Alert", message: self.responsevalue, preferredStyle: UIAlertController.Style.alert)
let action1 = UIAlertAction(title: "Ok",style: .default)
{(ACTION)in
}
alert1.addAction(action1)
self.present(alert1, animated: true, completion: nil)
}catch let parsingError {
print("Error", parsingError)
let alert1 = UIAlertController(title: "Alert", message: parsingError as? String, preferredStyle: UIAlertController.Style.alert)
let action1 = UIAlertAction(title: "Ok",style: .default)
{(ACTION)in
}
alert1.addAction(action1)
self.present(alert1, animated: true, completion: nil)
}
}
task.resume()