Key points about Properties in Swift

Rushabh Singh
Naukri Engineering
Published in
3 min readAug 21, 2021
Photo by AbsolutVision on Unsplash

Note: This article is only for revising key points that we might forget or miss or are unaware of.

For basic understanding, please refer to the official documentation of Swift.

There are different types of Properties in Swift,

1) Stored properties: Store constant/variable values as part of an instance of a particular class or structure.

2) Computed properties: Calculate (rather than store) values as part of an instance using getter and optional setter.

3) Type properties : Associated with Type rather than instance of a type.

4) Lazy stored property: A property whose initial value isn’t calculated until the first time it’s used.

We do not have a Lazy Computed property in swift. But it can be achieved using closure for certain required scenarios.

Important points about:

a. Stored and Computed properties

Computed properties are recalculated every time you access them.

Computed properties are provided by classes, structures, and enumerations.

Stored properties are provided only by classes and structures.

Why do enums have computed properties but not stored properties?

Also, why is it that Extension can add new computed properties but does not support Stored properties?

Have you ever thought about why it is so? Will try to cover it in the next post.

b. Lazy Properties

Lazy properties should always be declared as “var” and not “let”

Reason: First let’s understand why we need lazy properties?

  1. It is useful when the initial value for a property requires complex or computationally expensive setup that shouldn’t be performed unless or until it’s needed.
  2. It is useful when the initial value of a property is dependent on outside factors whose values aren’t known until after an instance’s initialization is complete.

Now let us focus more on the 2nd reason.

As Constant properties must always have a value before initialization completes, they cannot be lazy properties.

And since Lazy property’s initial value might not be retrieved until after instance initialization completes, it has to be declared as var.

Unlike Computed properties, Lazy properties are only calculated once when they’re first accessed.

But if a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property hasn’t yet been initialized, there’s no guarantee that the property will be initialized only once.

c. Global and Local properties

Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties.

Unlike lazy stored properties, global constants and variables don’t need to be marked with the lazy modifier.

Local constants and variables are never computed lazily.

d. Type properties

Since it belongs to the Type, there will only ever be one copy of these properties, no matter how many instances of that type you create.

Unlike stored instance properties, you must always give stored Type properties a default value. This is because the type itself doesn’t have an initializer that can assign a value to a stored type property at initialization time.

Stored type properties are lazily initialized on their first access. They’re guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they don’t need to be marked with the lazy modifier.

e. Property Observers

Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.

You can add property observers in the following places:

  • Stored properties that you define
  • Stored properties that you inherit
  • Computed properties that you inherit

For a computed property that you define, use the property’s setter to observe and respond to value changes, instead of trying to create an observer.

--

--

Rushabh Singh
Naukri Engineering

Moving fast without breaking things 👨‍💻……. Exploring Mobile Apps development