私はsthを多分奇妙にしようとしていますが、私は次の問題をスタックしています。非同期閉包変数を持つ構造関数
私は、非同期関数を設定するclosureプロパティを持つ構造体を持っています。
私が望むのは、この非同期関数を呼び出して戻り値を取得することです。
私はこれを回避策で解決できますが、正しい方法で解決したいと思います。
何か助けていただければ幸いです。奇妙な重複に
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