More about Structs in Swift
By: Agnel Selvan
Tags: Swift, Swift Programming, Swift 5, iOS, Struct

Structs are like complex data types, which means it holds multiple data types. You can create the instance of struct and fill in the values and you can access multiple values inside the struct.

Properties in struct

The variables declared inside the struct are called properties. Let’s create a struct of Person and pass in two properties name and age

Here, swift makes it very easy for us because it automatically generates code for memberwise initializer. This means you can pass the initial value for the two properties like this

Whereas in class, you need to call the init() for passing the value.

Now let’s try to read the properties by simply putting a print statement

Methods in struct

Not only properties, but You can also define a method (functions) too inside a struct.

Functions inside a struct are called methods and here I have created a method aboutMe() which prints the user details. and you just need to call the method by .aboutMe().

Copying a struct

Assigning a value one struct to another struct is noting but copying a struct and storing in another variable, behind the scene swift duplicates the struct. Well, that's not strictly true. In struct swift uses the technique called “Copy on Write”, which means the duplicate of struct is created only if we change the struct value. Let's try it with one example.

In this, you can clearly see that I have copied the firstPerson struct to firstPersonCopy and changed the name from John D’souza to James.

Property Observer

Swift lets us know when a particular property inside the struct is going to change or did change. This an actually a good way to have a user interface updated when the value changes.

There are two property observer willSet and didSet they are called when the property value going to change and after the value is changed. In willSet, Swift allows you to access a variable named newValue where the new value which we are going to assign can be accessed in newValue, and in didSet, swift allows us to access a variable named oldValue where the old value which we are going to replace will be accessed in oldValue.

Here, you can clearly see the property observer willSet and didSet. In willSet, you might have noticed a variable called newValue. In this variable, you can access the value which you are going to change. After the age value gets changed this didSet observer will be called and in the oldValue you can access the old Value.

Computed Properties

A computed property provides indirect access to other properties and values. It is used to derive value from other properties when the instance of the struct is created. Let's look into an example

Here, I have created an id which is a computed property. The value of the id is generated when the instance of struct is created.

Static Properties and methods

Swift allows us to create property and methods that belong to a struct, rather than an instance of the struct. This means you can access the properties or methods even if the instance of struct is not created. These shared properties in swift are called static properties. You can create a static property simply by mentioning static before a variable.

Here, I have created a static variable role as a student and you can access this property when without creating the instance of the struct.

Lazy properties in Struct

Swift allows you to create some properties only when needed. Let's understand by performing an example.

Here, I have created a struct Student and I have accessed inside the Person struct. When the instance of Person struct is created. You might have noticed the Student struct has also been initialized. This will consume more memory as your app grows bigger. So to avoid this you need to use lazy before a variable.

Here you can notice I have added a lazy keyword before student property this is because the instance of the student struct is initialized only when we call the particular property which we assigned for that struct i.e student property.

Mutating methods in struct

If the instance of a struct is constant you cannot change the value of the property because the instance of the struct itself is constant. So to avoid this kind of issue you can create a mutating method to change the value of properties.

Here, changeName() mutating method is created for changing the name.