DEV Community

Maegan Wilson
Maegan Wilson

Posted on • Updated on • Originally published at blog.appsbymw.com

Stored Properties in Swift

This post is going to define what a stored property is and how to use it.

There is a Swift Playground with examples in this post. It is at this link. Feel free to copy the files or clone the entire repo, but the repo has more than just this post's sample code.

Topics covered in this post

  • What is a Stored Property
  • How to create a Stored Property
  • Changing Stored Properties

What is a Stored Property?

According to the documentation:

A stored property is a constant or variable that is stored as part of an instance of a particular class or structure

A stored property is a value that is in a class or structure as a variable or constant.

How to create a Stored Property

  1. Create a structure or class
  2. Declare a variable or constant in the class

Example of Stored Properties

struct Name {
    var firstName: String
    var lastName: String
}
Enter fullscreen mode Exit fullscreen mode

In the example, there are 2 properties, firstName and lastName.

Now, to use the properties.

var myName: Name = Name(firstName: "Maegan", lastName: "Wilson")

myName.firstName // "Maegan"
Enter fullscreen mode Exit fullscreen mode

Changing Stored Properties

As long as the properties are variables and used on an instance that is not a constant, the values can be changed.

If the properties were constants, then they could not be changed. If the instance of a structure is assigned to a constant, then the properties cannot be changed. If the instance of a class is assigned to a constant, then the properties can be changed. This difference is due to the value (structures) vs. reference (classes) types. When a value type is declared as a constant, then so are all the properties. It does not matter if the properties were declared as variables or constants. When a reference type is declared as a constant, then the properties that are variables can be changed for that instance.

Reminder

  • A structure is a value type.
  • A class is a reference type.

Example of Changing Stored Properties in a Struct

Notice that myName was declared as a variable, and the properties of Name are variables as well. Since everything is a variable, the properties of myName can still be changed.

myName.firstName = "Meg"
myName.lastName = "Wil"

myName.firstName // Meg
Enter fullscreen mode Exit fullscreen mode

In the following example, running the code will cause an error.

let dogsName: Name = Name(firstName: "Thorgi", lastName: "Wilson")

dogsName.firstName // Thorgi
dogsName.lastName = "Wil"

Enter fullscreen mode Exit fullscreen mode

The error should say Cannot assign to property: 'dogsName' is a 'let' constant. This error is because dogsName is declared as a constant.

Example of Changing Stored Properties in a Class

class Pet {
    var name: Name
    var type: String

    init(name: Name, type: String){
        self.name = name
        self.type = type
    }
}

let dobby: Pet = Pet(name: Name(firstName: "Dobby", lastName: "Wilson"), type: "dog")
dobby.type // dog
dobby.type = "cat"
dobby.type // cat
Enter fullscreen mode Exit fullscreen mode

This example does not produce an error because Pet is a class and is, therefore, a reference value.


If you enjoy my posts, please consider sharing it or Buying me a Coffee!

Buy Me A Coffee

Top comments (0)