Hi guys
I'm going through a book on closures and I have a question about it.
This author tries to explain the need for using the weak keyword. Doing so he states '‘If self is not nil it will capture a strong reference and make sure it sticks around until the print statement finishes'.
I am a bit confused now. This code is used to make sure that the object still exists to perform the feedback, and hence the print. This would imply that self not being nil no longer becomes a weak self but a strong one through the guard statement.
If that to be the case then the closure now points strongly to the Editor object. The Editor object points strong to the closure. So how can the Editor object still be deallocated? Or is a closure deallocated the moment it reached the end of its statements? Because I thought this was not the case. Moreover the Editor object points strongly to it?
Thanks for your help. I'm hurting my head over this.
I'm going through a book on closures and I have a question about it.
Code:
extension Editor {
func editTutorial(_ tutorial: Tutorial) {
queue.async() {
[weak self] in
guard let self = self else {
print("I no longer exist so no feedback for you!")
return
}
DispatchQueue.main.async {
print(self.feedback(for: tutorial))
}
}
}
}
This author tries to explain the need for using the weak keyword. Doing so he states '‘If self is not nil it will capture a strong reference and make sure it sticks around until the print statement finishes'.
I am a bit confused now. This code is used to make sure that the object still exists to perform the feedback, and hence the print. This would imply that self not being nil no longer becomes a weak self but a strong one through the guard statement.
If that to be the case then the closure now points strongly to the Editor object. The Editor object points strong to the closure. So how can the Editor object still be deallocated? Or is a closure deallocated the moment it reached the end of its statements? Because I thought this was not the case. Moreover the Editor object points strongly to it?
Thanks for your help. I'm hurting my head over this.