It looks like in Swift 4.2 I can write the code below and it compiles.
There was a proposal (SE-0079) to make this work which was deferred.
Is this now supported in the language to make a weak self into a strong self?
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
// Do stuff with self, knowing it is held strongly after the guard statement
}
2 Likes
jrose
(Jordan Rose)
2
Oops. It wasn't supposed to be supported, but I suppose we're going to have to stay source-compatible. @Slava_Pestov, @rintaro, any idea how this changed?
bzamayo
(Benjamin Mayo)
3
AFAIK this was discussed in this thread, and I believe the behaviour was enabled in this PR. I'm not sure why that proposal (which predates the more recent thread) still says 'Deferred' though.
2 Likes
Thank you! Glad it's supposed to be in 4.2.
jrose
(Jordan Rose)
5
My mistake! Thanks, Benjamin.