我将结果作为像这样的字符串数组
["India","America","Australia","China","Russia"]
我正在使用 Alamofire 来使用代码获取响应。没有错误,但我得到的结果为null。请帮助解析这个。
sessionManager?.request(strURL, method: method, parameters: params, encoding: encoding , headers: headers).responseJSON { (response) in
switch response.result {
case .success:
let resJson = JSON(response.result.value!)
success(resJson)
break
case .failure(let error):
failure(error as NSError)
break
}
}
答案 0 :(得分:2)
试试这个:
if let responseData = response.result.value{
let responsevalue = responseData as? [String]
}
答案 1 :(得分:0)
对于寻找另一个派生答案的人,只需将这段代码放在Alamofire.request(...)
之后:
.responseJSON(completionHandler: { (response) in
switch response.result{
case .success(let value):
// Here is your array of String
let arrayOfStrings = value as? [String]
case .failure(let error):
// Some code when error happens...
print(error.localizedDescription)
}
})
答案 2 :(得分:0)
使用SwiftyJSON的此解决方案:
.responseJSON(completionHandler: { (response) in
switch response.result{
case .failure(let error):
print(error.localizedDescription)
case .success(let res):
let json = JSON(res)
let res = json["result"]
var models = [String]()
if let models1 = company["models"].array {
for model in models1 {
guard let mod = model.string else { return }
models.append(mod)
}
}
}
})