Заменить
let jsonString = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
на
if let result = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]] {
for item in result {
print(item["name"])
}
}
ответом является массив, а не словарь, также не используйте NS stuff
NSDictionary => [String: Любой]
NSUrl => URL
NSMutableURLRequest => URLRequest
func fetchCountryList(countryURL:URL, completion:@escaping (_ arr :[[String:Any]]?, _ error:Error?) -> ()) {
print(countryURL)
let request = URLRequest( url: countryURL)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
do{
if let data = data,
let jsonString = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]]
, error == nil {
completion(jsonString,nil)
} else {
print("error=\(error!.localizedDescription)")
let errorDict = ["error_status":true,"message":error!.localizedDescription] as [String : Any]
completion(nil,error)
}
}
catch{
print("error=\(error.localizedDescription)")
let errorDict = ["error_status":true,"message":error.localizedDescription] as [String : Any]
completion(nil,error)
}
}
task.resume()
}
Вызов
serviceModel.fetchCountryList(countryURL:countryURL){ (result,error) in
if let res = result {
print(res)
}
}