【发布时间】:2019-07-18 01:58:15
【问题描述】:
我尝试通过 Alamofire 发送请求以从 Amazon 获取 JSON,但它是异步的。在得到亚马逊的响应之前,它会返回调用者函数。
public func getJSON(fileName: String) -> JSON?{
let url = "http://s3.eu-west-3.amazonaws.com" + fileName
print(self.json)
if self.json == nil {
Alamofire.request(url)
.responseJSON { response in
if let result = response.result.value {
self.json = JSON(result)
}
}
return self.json
}
else{
return nil
}
}
public func initTableView(){
let myJson = AmazonFiles.shared.getJSON(fileName: "/jsonsBucket/myJson.json")
print(myJson["id"])
}
initTableView 函数中的对象myJson 始终为零。
我该如何解决这个问题?
【问题讨论】:
标签: ios swift asynchronous alamofire