I want a lazily-initialized property whose initializer I can invoke again if I set the property to nil.
If I define my property this way:
lazy var object = { /*init code*/ }()
...and later invoke the property, the initializer is triggered once. However, if I set object
to nil later in my program, the initializer is not invoked again. How can I do that in Swift?
I looked into computed properties but they don't actually store values, so whenever I invoke the variable, the computation or initialization always occurs. I want to compute only whenever the property is nil.
Here's a lazy pattern I use when your object can only ever be
nil
or a computed value. It requires only 2 properties:todayPredicate
is constructed only once when it is read for the first time (lazy).So why would you ever want to set
todayPredicate
to nil? In this example you are probably observing for the day changing becausetodayPredicate
must always represent today. In your observer code you would simply do this, for example...The lazy property initializer is responsible of initializing the property the first time it is accessed in read mode. Setting to nil has no effect on the initialization status - it's just a valid value the property stores.
You can mimic a lazy initialization with 3 properties:
The code looks like this:
5
integer in the above examplemyProp
is an optional integer (to be able to store anil
):_myProp
property_myProp
isnil
, it invokes the initializer, assigning it to_myProp
, and it returns its valueIf you want to reuse that pattern, it's better to put everything in a class:
Note: a
struct
is not usable because setting a property inside a property getter is not allowed, whereas in a class it is.Then you can use it as follows:
Some tests in playground:
The downside is that you have to access to the actual property as
x.lazyProp.value
and not asx.lazyProp
.