Swift 键路径的Alamofire responseArray字符串数组
我有一个rest调用,它返回键路径数据的字符串数组[string],例如Swift 键路径的Alamofire responseArray字符串数组,swift,alamofire,objectmapper,Swift,Alamofire,Objectmapper,我有一个rest调用,它返回键路径数据的字符串数组[string],例如 { "data": [ "1", "3", "5", "2", "4", "9" ] } 我试图通过responseArraykeyPath:data获取它,但在中获取*.responseArraykeyPath:data{response:DataResponse行的编译错误* 无法将“DataResponse->”类型的值转换为预期的参数类型“DataRespo
{
"data": [
"1",
"3",
"5",
"2",
"4",
"9"
]
}
我试图通过responseArraykeyPath:data获取它,但在中获取*.responseArraykeyPath:data{response:DataResponse行的编译错误*
无法将“DataResponse->”类型的值转换为预期的参数类型“DataResponse->Void”
请求示例的一部分
alamofireManager.request(url)
.responseArray(keyPath: "data") {(response: DataResponse<[String]>) in
if response.result.isSuccess {
if let data = response.result.value {
//process success
} else {
// handle error
}
}
...
有人知道怎么做吗?问题是字符串不可映射。根据,以下是建议的解决方案: 在这种情况下,我建议直接从Alamofire响应访问数据 或者,您可以将String子类化,并使子类可映射,但是我认为这对于您的情况来说是不必要的 使用Swift的4个可编码无外部依赖项:
struct APIResponse: Decodable {
let data: [String]
}
let url = "https://api.myjson.com/bins/1cm14l"
Alamofire.request(url).responseData { (response) in
if response.result.isSuccess {
if let jsonData = response.result.value,
let values = try? JSONDecoder().decode(APIResponse.self, from: jsonData).data {
//process success
print(values)
} else {
// handle error
}
}
}