Properties in Swift

Janvi Arora
2 min readFeb 28, 2023

--

According to Apple documentation, properties access stored and computed values that are part of an instance or type.

Generally, properties are of two types:

  1. Stored property
  2. Computed property

Stored Property:

A constant or variable that’s stored as part of an instance of a particular class or structure.

  1. Variable stored property
  2. Constant stored property
  3. Lazy stored property
// Can be changed
var variableStoredProperty: Int = 10

// Can not be changed
let constantStoredProperty: Int = 10

// a property whose initial value isn’t calculated until the first time it’s used
lazy var lazyStoredProperty: Int = 10

Why should a variable be declared as a lazy property?

Because the variable’s initial value might not be retrieved until after instance initialization completes.

Why a constant can not be declared as a lazy property?

Constant properties must always have a value before initialization completes, and therefore can’t be declared as lazy.

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.

Computed Property:

Computed properties are properties that do not actually store a value. Instead, they provide a getter and/or setter to retrieve and/or modify other properties and values indirectly.

Computed properties can be defined using the get and set keywords, or by providing a closure as the property's value. Here is an example of a computed property using get and set:

class Circle {
var radius: Double = 0.0

var diameter: Double {
get {
return radius * 2
}
set(newDiameter) {
radius = newDiameter / 2
}
}
}

In this example, the diameter property does not actually store a value but instead calculates it based on the value of radius. The get block calculates the diameter, while the set block allows the diameter to be set by updating the radius.

Computed properties are useful for encapsulating complex calculations or transformations of existing data in a simple and concise way.

Shorthand Setter Declaration:

It allows you to provide a short syntax for the setter of a computed property. Instead of using the traditional setter declaration, which is a function that takes a new value as a parameter and assigns it to the appropriate property, you can use the newValue keyword to represent the new value being assigned.

var name: String {
get {
return "Hello"
}
set {
print("The new value is \(newValue)")
}
}

// now you can set the value of name
var person = Person()
person.name = "John"

// Output: The new value is John

Shorthand Getter Declaration:

It is used to implement a read-only computed property. This is a more concise way of defining a computed property that doesn’t have a setter.

struct Rectangle {
var width: Double
var height: Double

var area: Double {
return width * height
}
}

--

--

No responses yet