gpt4 book ai didi

ios - Swift 主线程不起作用

转载 作者:行者123 更新时间:2023-11-28 06:53:31 25 4
gpt4 key购买 nike

我试图在从服务器获取 JSON 数据后更新 UILabel。我已经尝试了很多方法来修复它,但它仍然不起作用。

这是我的代码

class ShopInformation: UIViewController {
var latitude = String();
var longitude = String();
var shopInformation:JSON = nil;

@IBOutlet weak var name: UILabel!

@IBOutlet weak var addres: UILabel!
override func viewDidLoad() {

super.viewDidLoad()
let url = NSURL(string: "http://localhost/whatapro/json/shopinformation.php?latitude=\(self.latitude)&longitude=\(self.longitude)")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
(data, response, error) in
// print(NSString(data: data!, encoding: NSUTF8StringEncoding))
self.shopInformation = JSON(data: data!)
print(self.shopInformation["response_data"][0])

dispatch_async(dispatch_get_main_queue()) {

self.name.text = self.shopInformation["response_date"][0]["name"].string
self.addres.text = self.shopInformation["response_date"][0]["address"].string
}

}

task.resume()
}
}

我也试过这种方式

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
dispatch_async(dispatch_get_main_queue()) {
self.name.text = self.shopInformation["response_date"][0]["name"].string
self.addres.text = self.shopInformation["response_date"][0]["address"].string
}
}

谢谢

最佳答案

展开您的代码,将 JSON 中的值分解为一个单独的变量。分配变量后设置断点,然后检查调试器中的值,并检查 self.nameself.addres 导出的值。

我的猜测是您的一个或两个 socket 未连接。

(您的代码的第一个版本,使用 dispatch_async(dispatch_get_main_queue()),看起来是正确的。)

关于ios - Swift 主线程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295633/

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