gpt4 book ai didi

ios - 如何使用viewdidload()中的回调函数?

转载 作者:行者123 更新时间:2023-11-29 05:48:24 24 4
gpt4 key购买 nike

我得到了一个带有函数(requestSitesData)的类(Client),它可以从服务器获取数据,并且其中有@escaping:

func requestSitesData(response: @escaping (_ data: Data?, _ error: Error?) -> Void) {
DispatchQueue.global(qos: .default).asyncAfter(deadline: .now() + randomTimeDelay()) {
guard self.isRequestSuccessful() else {
response(nil, ClientError.requestFailed)

return
}
guard let fileURL = Bundle.main.url(forResource: self.responseFilename, withExtension: "json") else {
response(nil, ClientError.noFile)
return
}
do {
let data = try Data(contentsOf: fileURL)
let _ = try JSONSerialization.jsonObject(with: data)
response(data, nil)
} catch let error {
response(nil, error)
}
}
}

所以我认为这里的响应函数是一个回调函数。在我的项目中,我有一个全局变量来存储来自服务器的数据,并希望将其显示在我的 View Controller 上。但似乎无论我将 call_back 函数放在 viewcontroller 类中的哪个位置,回调函数都会在所有函数之后调用(应该如此),所以我无法在 viewdidload() 中获取数据。

func call_back(data: Data?, error: Error?) -> Void {
print("call_back called")
if let file_data = data {
if let sites_map = try? JSONSerialization.jsonObject(with: file_data, options: .mutableLeaves) as! [String: [Dictionary<String, Any>]] {
if let sites = sites_map["sites"] {
for site in sites {
if let name = site["name"] as? String,
let slogan = site["slogan"] as? String,
let url = site["url"] as? String,
let description = site["description"] as? String,
let color = site["color"] as? String {
let single_site: Site = Site(name: name, slogan: slogan, url: url, description: description, color: color)
sites_res.append(single_site)
}
}
}
}
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let c = Client()
c.requestSitesData { (response_data, response_error) in
self.call_back(data: response_data, error: response_error)
print("ddd")
}

}

我不知道viewdidload()中如何获取数据,是不是因为我调用这个call_back函数的方式不对?谁能帮忙?

最佳答案

修改函数如下:

func call_back(data: Data?, error: Error?) -> [String: [Dictionary<String, Any>]]? {
print("call_back called")
if let file_data = data {
if let sites_map = try? JSONSerialization.jsonObject(with: file_data, options: .mutableLeaves) as? [String: [Dictionary<String, Any>]] {
return sites_map
}
}
return nil
}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let c = Client()
c.requestSitesData { (response_data, response_error) in
if let sites_map = self.call_back(data: response_data, error: response_error){
if let sites = sites_map["sites"] {
for site in sites {
if let name = site["name"] as? String,
let slogan = site["slogan"] as? String,
let url = site["url"] as? String,
let description = site["description"] as? String,
let color = site["color"] as? String {
let single_site: Site = Site(name: name, slogan: slogan, url: url, description: description, color: color)
sites_res.append(single_site)
}
}
}
}
}
}

关于ios - 如何使用viewdidload()中的回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55930694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com