【问题标题】:Cannot convert value of type() to closure result type NSDictionary in Swift 2无法在 Swift 2 中将 type() 的值转换为闭包结果类型 NSDictionary
【发布时间】:2016-01-07 04:34:43
【问题描述】:

我必须返回这个函数的一个值。我在这一行出现错误

func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> ()) {
    getResonse(url, completionhandler: { (dict) -> NSDictionary in
       completionHandler(stationDictionary: dict) // Error on this line 
    })
}

【问题讨论】:

  • 您能打印出您收到的回复吗?
  • 我的代码没有执行他们显示错误
  • completionHandler(stationDictionary: dict) as? NSDictionary 当我使用此行代码执行获取响应但在此行崩溃
  • 试试completionHandler(stationDictionary: dict as? NSDictionary)
  • 你能透露网址吗?您还使用任何框架吗?

标签: swift swift2


【解决方案1】:

这肯定行得通。

func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> NSDictionary) {
    getResonse(url, completionhandler: { (dict) -> NSDictionary in
       completionHandler(stationDictionary: dict) // Error on this line 
    })
}

并像使用它一样,

var dict = NSDictionary()
temp.GetStation("your url") { (stationDictionary) -> NSDictionary in
    dict = stationDictionary;
    print("your dictionary := \(stationDictionary)")
}

【讨论】:

  • 对不起,我赞成您的回答并破坏了您的“666”声誉。 ;)
【解决方案2】:

实际上你可能弄错了 getResonse 的代码... 应该像下面这样..

func getResonse(url : String, completionHandler: (dictionary:NSDictionary) -> Void) {
}

所以你应该像下面这样调用 GetStation...

func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> NSDictionary) {
    getResonse(url) { (dictionary) -> Void in
        return completionHandler(stationDictionary: dictionary)
    }
}

【讨论】:

  • 嗨..这个答案有效吗?应该。因为我已经在xcode中测试过。
【解决方案3】:
func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> ()) {
    getResonse(url) { (dict) -> NSDictionary in
        completionHandler(stationDictionary: dict)
        return dict
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多