例如
func hello() -> Bool {
var result = false
httpRequestWithCompletionHandler({ is404 in
result = is404
})
return result
}
這個函數的回傳值永遠是 false,因為 closure 是異步執行的
怎麼在呼叫這個函數的時候,得到真正的回傳值呢?
註: result 必須定義在函數體內,不能定義在函數體外
大家讲道理2017-04-24 09:12:38
var result_cb = function (result) { alert(result) };
function hello(cb) {
httpRequestWithCompletionHandler(is404) {
cb(is404);
}
}
hello(result_cb);
這樣?