我经常这样做,
let when = DispatchTime.now() + 2.0
DispatchQueue.main.asyncAfter(deadline: when) {
beep()
}
Run Code Online (Sandbox Code Playgroud)
在一个应用程序中,我们经常这样做
tickle.fresh(){
msg in
paint()
}
Run Code Online (Sandbox Code Playgroud)
但如果你做这个
let when = DispatchTime.now() + 2.0
DispatchQueue.main.asyncAfter(deadline: when) {
tickle.fresh(){
msg in
paint()
}
}
Run Code Online (Sandbox Code Playgroud)
当然,你必须做这个
let when = DispatchTime.now() + 2.0
DispatchQueue.main.asyncAfter(deadline: when) { [weak self] _ in
tickle.fresh(){
msg in
self?.paint()
}
}
Run Code Online (Sandbox Code Playgroud)
或者,也许这个
let when = DispatchTime.now() + 2.0
DispatchQueue.main.asyncAfter(deadline: when) {
tickle.fresh(){
[weak self] msg in
self?.paint()
}
}
Run Code Online (Sandbox Code Playgroud)
或许这个
let when = DispatchTime.now() …Run Code Online (Sandbox Code Playgroud) 如果我有一个闭包传递给这样的函数:
someFunctionWithTrailingClosure { [weak self] in
anotherFunctionWithTrailingClosure { [weak self] in
self?.doSomething()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我[weak self]在someFunctionWithTrailingClosure自己的捕获列表中声明自己没有重新声明它weak再次在捕获列表中anotherFunctionWithTrailingClosure self已经成为一种Optional类型但是它也成为一个weak参考?
谢谢!
closures weak-references automatic-ref-counting swift unowned-references