Swift 2中无法将type()的值转换为闭包结果类型NSDictionary



我必须返回此函数的值。我在这行中出错

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

这肯定会奏效

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

实际上,您可能会为getResolense编写错误的代码。。。应该是如下所示。。

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)
    }
}
func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> ()) {
    getResonse(url) { (dict) -> NSDictionary in
        completionHandler(stationDictionary: dict)
        return dict
    }
}

最新更新