代码改变世界

Closure use of non-escaping parameter 'xxx' may allow it to escape

2019-02-21 11:39  法子  阅读(2446)  评论(0编辑  收藏  举报

新版的Swift闭包做参数默认是@noescaping,不再是@escaping。所以如果函数里异步执行该闭包,要添加@escaping。否则报错:Closure use of non-escaping parameter 'xxx' may allow it to escape.

func delay(seconds: Double, completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
        completion()
    }
}