'Lazy initialization and deinit
I would to know if it's possible, in my view controller, use a lazy property and in deinit method call a method of my lazy property only if it was initialized. Below some code:
fileprivate lazy var session: MySession = {
let session: MySession = MySession()
session.delegate = self
return session
}()
deinit {
session.delete()
}
In this way, when session.delete() in the deinit method is called and session hasn't been used (so is still nil), it's initialized and then delete is called. I don't want this. I'd like to call delete only if session had been previously initialized.
Is there a way to achieve this? Have I to let go the lazy initialization idea?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
