I am trying out to figure out how does capturing in Swift closures work.
Let's assume the following code example
class Worker {
let block: () -> Void
let id: String
init(id: String, block: @escaping () -> Void) {
self.id = id
self.block = block
}
}
var worker: Worker!
worker = Worker(id: "1", block: { _ in
print(worker.id)
})
At the moment the closure is being created, the worker variable is still nil. However, the closure will successfully capture the worker variable.
How does that work? How does the closure keep a strong reference to a variable that still not being initialized?