Swift错误:对genericstypes字典的引用需要<…>中的参数

Reference to generic type Dictionary requires arguments in <...>的错误Reference to generic type Dictionary requires arguments in <...>出现在函数的第一行。 我想这个函数返回一个从api检索到的NSDictionary。 任何人都知道这里可能会发生什么?

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

编辑:

第二期:

  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' } }) } 

如果您打算返回一个字典,那么您需要指定其中的键和数据的types。

例如:如果你的键和值都是string,那么你可以写如下的东西:

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

如果您不确定其中的数据,或者您有多种types的数据,请将返回types从Dictionary更改为NSDictionary

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

要么

你可以这样写:

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