0

I'm trying to implement an asynchronous function as told in this topic but I always get the following error from Xcode : Type 'dispatch_queue_t!' does not conform to protocol 'OS_dispatch_queue'

Here's my 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)

}

Here's the lookup function:

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()

}

Any hint on what I could be doing wrong?

Community
  • 1
  • 1
Édouard
  • 3
  • 1
  • 6

1 Answers1

0

I figure it out by myself. There was a bug inside on my code.

On the line

priceShare = price

I needed to put

priceShare = Double(price)!

since priceShare require a Double. Don't understand why Xcode didn't tell me so.

Édouard
  • 3
  • 1
  • 6
  • The Swift compiler's errors are often horrible and confusing. Assume the error means "Derp? Something is wrong!" and the specifics are meaningless. Then go find the error yourself, as you've done. – Duncan C Aug 13 '15 at 11:30