J'essaie de mettre à jour UILabel après avoir obtenu des données JSON du serveur. J'ai essayé plusieurs façons de le réparer, mais cela ne fonctionne toujours pas.Le thread principal Swift ne fonctionne pas
Voici mon code
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()
}
}
J'ai aussi essayé cette façon
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
}
}
Merci
Où et quand sont initialisés vos étiquettes? – Wez
Un autre point comprenant ce que @Wezly a mentionné est si vous avez vérifié les valeurs reçues de la requête réseau? – EridB
J'ai mis à jour le code. Pour les données je peux le récupérer du serveur –