Je suis en train de mettre en œuvre une fonction asynchrone dit in this topic mais je reçois toujours l'erreur suivante de Xcode: Type 'dispatch_queue_t!' does not conform to protocol 'OS_dispatch_queue'
Swift ne se conforme pas au protocole « OS_dispatch_queue »
Voici mon code:
@IBAction func buyButton(sender: AnyObject) {
// get wanted symbol
let symbol = symbolField.text!
// get price of share
var priceShare :Double = 0
_ = lookup(symbol) { name, symbol, price in
dispatch_async(dispatch_get_main_queue()) {
priceShare = price
}
}
buy(symbol, number: 1, price: priceShare)
}
Voici la fonction de recherche:
func lookup(entry : NSString, completion: ((name :String, symbol :String, price :String) -> Void)) {
// define return values
var name = String()
var symbol = String()
var price = String()
// define URL
let url = NSURL(string: "http://yahoojson.gobu.fr/symbol.php?symbol=\(entry)")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
name = jsonResult["name"] as! String
symbol = jsonResult["symbol"] as! String
price = jsonResult["price"]!!.stringValue as String
completion(name: name, symbol: symbol, price: price)
} catch {
print(error)
}
}
}
// run the task
task.resume()
}
Un conseil sur ce que je pourrais faire mal?
Les erreurs du compilateur Swift sont souvent horribles et confuses. Supposons que l'erreur signifie "Derp? Quelque chose ne va pas!" et les détails sont dénués de sens. Ensuite, allez chercher l'erreur vous-même, comme vous l'avez fait. –