The problem is that you need to return SOMETHING from getForecast -- right now 
you're returning nothing.

The task will execute at some point "in the future" after the routine ends.  An 
"easy" solution is to pass a closure to getForecast that will take a 
Dictionary? as a parameter (just in case there's an error, make the parameter 
an optional).  Your task then calls that closure with the resulting Dictionary.

Welcome to asynchronous programming!

-- 
Glenn L. Austin, Computer Wizard and Race Car Driver         <><

> On Dec 22, 2015, at 6:25 AM, Eric E. Dolecki <edole...@gmail.com> wrote:
> 
> I am trying to return a dictionary from a method that uses a queue. I'm not
> sure how to actually return though as I get an error (can't convert value
> of type '() -> Dictionary<String,String>' to expected argument type
> 'dispatch_block_t'. I've tried a number of things but none prevent an error
> somewhere. I could ditch the queue altogether, but it feels better to use
> one.
> 
> - Eric
> 
> 
> func getForecast(cityName:String) -> Dictionary<String,String> {
>        let baseUrl: String = "
> https://urldefense.proofpoint.com/v2/url?u=http-3A__api.openweathermap.org_data_2.5_forecast&d=BQIFaQ&c=Hw-EJUFt2_D9PK5csBJ29kRV40HqSDXWTLPyZ6W8u84&r=GBXZhacxqultU1tKBDt744IExU5-txpVlucNMcizFfQ&m=tP_xkrFwAs-9Qn22yQLRdAJiwAh5yxUlmqG-ZbklzuE&s=W-iZmEs7W3Hf-MPt3D4_XkNcpy9J8LIp8HXEjh7aL1o&e=
>  "
>        let url: String =
> "\(baseUrl)?q=\(theCityName)&units=imperial&appid=\(APIKey)"
>        let finalUrl: NSURL = NSURL(string: url)!
>        let session = NSURLSession.sharedSession()
>        let task = session.dataTaskWithURL(finalUrl, completionHandler:
> {data, response, error -> Void in
>            if error != nil{
>                print(error!.localizedDescription)
>            }
>            var err: NSError?
>            let qualityOfServiceClass = QOS_CLASS_BACKGROUND
>            let backgroundQueue =
> dispatch_get_global_queue(qualityOfServiceClass, 0)
>            dispatch_async(backgroundQueue, {
> 
>                let json = JSON(data: data!, options:
> NSJSONReadingOptions(), error: &err)
>                //print("response is \(json)")
>                let weatherCurrent = json["list"][0]["main"]["temp"]
>                let weatherHigh = json["list"][0]["main"]["temp_max"]
>                let weatherLow = json["list"][0]["main"]["temp_min"]
>                let conditionForecast =
> json["list"][0]["weather"][0]["description"].stringValue
> 
>               * dispatch_async(dispatch_get_main_queue(), { () ->
> Dictionary<String,String> in*
> *                    print("Forecast: Current: \(weatherCurrent)ºF.
> H:\(weatherHigh), L:\(weatherLow). Cond:
> \(conditionForecast.capitalizedString).")*
> *                    let dict = ["current":"\(weatherCurrent)",
> "high":"\(weatherHigh)",*
> *                        "low":"\(weatherLow)",
> "conditions":"\(conditionForecast.capitalizedString)"]*
> *                    return dict*
>                })
>            })
>        })
>        task.resume()
>    }


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to