在传参时,如果closure
参数是在当前函数作用域执行完之前调用的,closure
是非逃逸闭包;如果是在作用域以外的地方调用,则是逃逸闭包。
在我们使用时,发现如果传参数是可选的闭包时,加上@escaping
会报错Closure is already escaping in optional type argument
static func dismiss(onCompletion: @escaping VoidFunction?) {
// 报错Closure is already escaping in optional type argument
}
原因是:
Actually, since Swift 3 has been released, the closure will be "escaped" if it's declared in enum, struct or class by default.
Obviously, Optional is enum. 所以可选类型的闭包参数本身就是逃逸闭包,不需要再加@escaping
网友评论