【问题标题】:Swift error: Reference to generic type Dictionary requires arguments in <...>Swift 错误:对泛型类型 Dictionary 的引用需要 <...> 中的参数
【发布时间】:2015-02-27 23:16:33
【问题描述】:

错误Reference to generic type Dictionary requires arguments in &lt;...&gt; 出现在函数的第一行。我试图让函数返回从 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'

        }
    })
    }

【问题讨论】:

    标签: ios iphone xcode swift dictionary


    【解决方案1】:

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

    例如:如果您的键和值都是字符串,那么您可以编写如下内容:

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

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

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

    你可以这样写:

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

    【讨论】:

    • 谢谢!它解决了第一个错误,但现在我收到一条消息,说“NSDictionary”不能转换为“Void”。你觉得这里发生了什么?
    • @user1851782:你从哪里得到错误?你现在使用什么代码?
    • @user1851782: 这是因为`let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in ` 它的类型是 void 并且是异步调用
    • “如果您不确定数据...将返回类型从Dictionary 更改为NSDictionary”——这不是一个好建议。如果你不确定类型,你应该弄清楚,不要使用不关心的类型。这只会在以后导致更多的混乱。如果你真的需要异构存储,你仍然应该使用字典,带有AnyAnyObject。仅当您实际需要 NSDictionary 时才应使用 NSDictionary
    • 是的——我真的是在说“除非你绝对必须,否则不要使用 AnyObject”,即使用特定类型,并附带“除非你需要,否则不要使用 NSObject” – 即在与 Obj-C API 交互时,或者如果它具有您不想没有的非常有用的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多