Since the Xcode 10.2 (Swift 5) the defer
statement at the end of the deinit
scope produces:
'defer' statement before end of scope always executes immediately; replace with 'do' statement to silence this warning
Let's take a look at this example:
var foo: String {
didSet {
// smt
}
}
deinit {
defer { <--- Warning
foo = bar
}
}
- Of course it's possible to get rid of this warning by moving the code from the observer to a method and call it explicitly but…
What's the point of this warning? - Isn't it reasonable to have the defer
statement in the deinit
? (e.g. to be able to trigger properties' observers).