Вам нужно
if let datas = dict["Data"] as? [[String:Any]] {
вместо
if let datas = dict["Data"] as? NSArray {
, поскольку элементы массива имеют тип Any
, на которые здесь нельзя подписаться data["PatientID"]
и data["DisplayName"]
Alamofire.request("request").responseJSON { (response) in
if let dict = response.result.value as? [String:Any] {
if let datas = dict["Data"] as? [[String:Any]] {
for data in datas {
if let id = data["PatientID"] as? Int , let name = data["DisplayName"] as? String {
let users = UserModel(UserID:id, Name: name)
}
}
}
}
}
Также рассмотрите возможность использования Codable
для анализа ответа
struct Root: Codable {
let data: [UserModel]
enum CodingKeys: String, CodingKey {
case data = "Data"
}
}
struct UserModel: Codable {
let patientID: Int
let displayName: String
enum CodingKeys: String, CodingKey {
case patientID = "PatientID"
case displayName = "DisplayName"
}
}
Alamofire.request("request").responseData{ (response) in
if let data = response.data {
do {
let res = try JSONDecoder().decode(Root.self, from: data)
print(res.data)
}
catch {
print(error)
}
}
}
Правильно
{"Данные": [{"DisplayName": "Джерри Смит", "DOB": "08.09.1987"}]}