Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift error: Reference to generic type Dictionary requires arguments in <...>

The error Reference to generic type Dictionary requires arguments in <...> is appearing on the first line of the function. I am trying to have the function return an NSDictionary retrieved from an api. Anyone know what could be going on here?

class func getCurrentWeather(longitude: Float, latitude: Float)->Dictionary?{

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apikey)/")
let forecastURL = NSURL(string: "\(longitude),\(latitude)", relativeToURL:baseURL)

let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

    if(error == nil) {
        println(location)
        let dataObject = NSData(contentsOfURL:location!)
        let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
        return weatherDictionary
    }else{
        println("error!")
        return nil

    }
})
}

EDIT:

Second Issue:

    class func getCurrentWeather(longitude: Float, latitude: Float)->NSDictionary?{

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apikey)/")
    let forecastURL = NSURL(string: "\(longitude),\(latitude)", relativeToURL:baseURL)

    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

        if(error == nil) {
            println(location)
            let dataObject = NSData(contentsOfURL:location!)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary


            return weatherDictionary //ERROR: NSDictionary not convertible to void

        }else{
            println("error!")
            return nil ERROR: Type void does not conform to protocol 'NilLiteralConvertible'

        }
    })
    }
like image 710
s_kirkiles Avatar asked Jan 01 '15 04:01

s_kirkiles


1 Answers

If you are planning to return a Dictionary then you need to specify the type of key and data in it.

Eg: If your key and value both are Strings then you can write something like:

class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, String>?
{
   ...
}

If you are not sure about the data in it or if you have multiple type of data, change the return type from Dictionary to NSDictionary.

class func getCurrentWeather(longitude: Float, latitude: Float) -> NSDictionary?
{
    ...
}

or

You can write like:

class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, AnyObject>?
{
   ...
}
like image 102
Midhun MP Avatar answered Oct 19 '22 01:10

Midhun MP