我试图返回responseJSON
max user process limit
但是,如果我尝试将responseJSON存储在" res"它不会工作:
static func getAllPersons(completionhandler:@escaping (Any) -> ()){
let URL = baseURL + "api/person"
Alamofire.request(URL).responseJSON {
response in
completionhandler(response.result.value as Any)
}
}
祝你好运
答案 0 :(得分:2)
response.result.value是可选的,所以尝试返回Any?。但这是更好的方法:
static func getAllPersons(completionhandler:@escaping ([String: Any]?, Error?) -> ()){
let URL = baseURL + "api/person"
Alamofire.request(URL).responseJSON {
response in
if let json = response.result.value as? [String: Any] {
completionhandler(json, nil)
}
else if let error = response.result.error as Error? {
completionHandler(nil, error)
}
}
}
答案 1 :(得分:1)
var res: Any = ""
PersonResource.getAllPersons{ (result) in
res = result
print(res)
}
将print语句放入其中将打印结果
为什么?
PersonResource.getAllPersons是一个异步调用,因此即使在PersonResource.getAllPersons的完成块执行并设置res之前也会执行print(res)