2015-08-12 1 views
0

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?

Répondre

0

Je me débrouille tout seul. Il y avait un bug à l'intérieur de mon code.

Sur la ligne

priceShare = price 

je devais mettre

priceShare = Double(price)! 

depuis priceShare ont besoin d'un double. Je ne comprends pas pourquoi Xcode ne me l'a pas dit.

+0

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. –