我经常这样做,
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) 假设我已经创建了一个弱自我使用
__weak typeof(self) weakSelf = self;
[self doABlockOperation:^{
...
}];
Run Code Online (Sandbox Code Playgroud)
在该块内,如果我嵌套另一个块:
[weakSelf doAnotherBlockOperation:^{
[weakSelf doSomething];
}
Run Code Online (Sandbox Code Playgroud)
它会创建一个保留周期吗?我是否需要为weakSelf创建另一个弱引用?
__weak typeof(self) weakerSelf = weakSelf;
[weakSelf doAnotherBlockOperation:^{
[weakerSelf doSomething];
}
Run Code Online (Sandbox Code Playgroud) memory-management block ios automatic-ref-counting retain-cycle
如果我有一个闭包传递给这样的函数:
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