迅捷错误:对泛型类型Dictionary的引用需要& lt; ...& gt;中的参数 [英] Swift error: Reference to generic type Dictionary requires arguments in <...>

查看:27
本文介绍了迅捷错误:对泛型类型Dictionary的引用需要& lt; ...& gt;中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误引用泛型类型Dictionary要求在函数的第一行出现< ...> 中的参数.我试图让函数返回从api检索到的NSDictionary.有人知道这里会发生什么吗?

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

    }
})
}

第二期:

    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'

        }
    })
    }

推荐答案

如果您打算返回字典,则需要在其中指定键和数据的类型.

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>?
{
   ...
}

如果不确定其中的数据或数据类型多种,请将返回类型从 Dictionary 更改为 NSDictionary .

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?
{
    ...
}

您可以这样写:

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

这篇关于迅捷错误:对泛型类型Dictionary的引用需要&amp; lt; ...&amp; gt;中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆