objective-c - 闭包导致的时间问题怎么解决?
伊谢尔伦
伊谢尔伦 2017-04-24 09:10:38
0
2
649

例如

func hello() -> Bool { var result = false httpRequestWithCompletionHandler({ is404 in result = is404 }) return result }

这个函数的返回值永远是 false,因为 closure 是异步执行的
怎么在调用这个函数的时候,得到真正的返回值呢?
注: result 必须定义在函数体内,不能定义在函数体外

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all (2)
大家讲道理
var result_cb = function (result) { alert(result) }; function hello(cb) { httpRequestWithCompletionHandler(is404) { cb(is404); } } hello(result_cb);

Like this?

    迷茫

    GO lang ? Asynchronously executed functions can only obtain values through callbacks, so the results cannot be obtained directly by returning bool from hello.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!