Found a bug in my code because of something very subtle:
class C: NSObject {
var foo: String = {
NSLog("non-lazy self: \(self)")
return ""
}()
lazy var bar: String = {
NSLog("lazy self: \(self)")
return ""
}()
}
let c = C()
let _ = c.bar
prints:
2020-04-11 13:38:50.989 Untitled 2[98985:8257392] non-lazy self: (Function)
2020-04-11 13:38:50.989 Untitled 2[98985:8257392] lazy self: <main.C: 0x7fe0b6d02ae0>
Bonus, code doesn't compile if C doesn't inherit from NSObject (so reproducible by replacing NSObject with say UIViewController). I encountered this because I had foo
which calls addTarget(_:action:)
with self as the target
and was wondering why the action never fires.