Swift2 以Swift格式返回值
我想调用另一个类中的函数。但他们并没有用swift返回字典。如何解决这个问题 在mySwift2 以Swift格式返回值,swift2,Swift2,我想调用另一个类中的函数。但他们并没有用swift返回字典。如何解决这个问题 在myViewController中 var dict = NSDictionary() dict = temp.GetStation(myUrl) 在我班上 func GetStation(url : String) -> NSDictionary { let dicts = NSDictionary() getResonse(url, completionhandler: { (dict)
ViewController中
var dict = NSDictionary()
dict = temp.GetStation(myUrl)
在我班上
func GetStation(url : String) -> NSDictionary {
let dicts = NSDictionary()
getResonse(url, completionhandler: { (dict) -> NSDictionary in
// print(dict)
return dict
})
return dicts;
}
试试这段代码。
func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> NSDictionary ) {
getResonse(url, completionhandler: { (dict) -> NSDictionary in
// print(dict)
completionHandler(stationDictionary: dict)
})
}
像这样使用它
var dict = NSDictionary()
temp.GetStation("your url") { (stationDictionary) -> NSDictionary in
dict = stationDictionary;
print("your dictionary := \(stationDictionary)")
}
我们添加了completionHandler
而不是返回值
,因为在函数GetStation
中,您正在调用函数getResonse
,该函数可能是异步的,可能正在调用webservice
因此,GetStation
的响应取决于getResonse
completionHandler(stationDictionary:dict)的响应。该行上出现错误。无法将type()的值转换为closure type nsDictionary。请检查编辑的答案,感谢它不起作用。现在检查代码的“并像这样使用”部分