@escaping
* 修饰 block
func testblock(_ sender: Any)
{
// escaping 逃逸闭包
// Escaping closure captures non-escaping parameter 'block'
// block(message) -> Reference to property 'message' in closure requires explicit 'self.' to make capture semantics explicit
/*block 生命周期将在函数声明周期结束后,依然保留,并且调用类函数,必须要用到self来确定关系*/
func doWorkAsync(_ block: @escaping(_ s: String) ->Void) {
print("block")
let temp = "临时变量"
message = "将要异步执行block"
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+30) {
self.message="异步修改message"
block(self.message)
block(temp)
}
message = "已经异步执行"
}
// @non-escaping 非逃逸闭包
// doWork 作用域不会出 testblock 作用域,无需指明 message中 self 的引用关系,因为 testblock函数 引用就有隐式的 self
func doWork(_ block:(_ s: String) ->Void) {
print("block")
block("123")
block(message)
block(self.message)
}
print("aaaaa");
doWork { (s) in
print(s)
}
doWorkAsync { (text) in
print(text)
}
print("bbbbbb")
}