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?
泛型类型字典的错误引用需要<…>出现在函数的第一行。我试图让函数返回从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
}
})
}
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'
}
})
}
1 个解决方案
#1
13
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
.
如果您对其中的数据不确定,或者您有多种类型的数据,请将返回类型从Dictionary更改为NSDictionary。
class func getCurrentWeather(longitude: Float, latitude: Float) -> NSDictionary?
{
...
}
or
或
You can write like:
你可以写:
class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, AnyObject>?
{
...
}
#1
13
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
.
如果您对其中的数据不确定,或者您有多种类型的数据,请将返回类型从Dictionary更改为NSDictionary。
class func getCurrentWeather(longitude: Float, latitude: Float) -> NSDictionary?
{
...
}
or
或
You can write like:
你可以写:
class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, AnyObject>?
{
...
}