我正在嘗試做某事也許很奇怪,但我與以下問題堆棧。異步關閉變量的結構函數
我有一個結構與閉包屬性,我設置異步函數。
我想要的是,調用這個異步函數來獲取返回值。
我可以用解決方法來解決這個問題,但我想用正確的方法來解決這個問題。
任何幫助表示讚賞。
這裏是我的代碼:
struct Item {
var myselector: (String -> Void)?
func getMeThat(completion: String -> Void) {
completion(myselector()) // error: cannot call value of non-function type String -> Void
}
}
class API {
class func requestThing(completion: String -> Void) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
completion("Kenan")
})
}
}
class ViewController {
func viewDidLoad() {
var item = Item()
item.myselector = { // error: cannot assign value of type() ->() to type String -> Void
API.requestThing({ (str: String) in
<#code#>
})
}
item.getMeThat({ (rtn: String) in
print(rtn)
})
}
}
可否請您直接編輯的錯誤信息到你的問題,而不是一個屏幕截圖? – NobodyNada