Swift Programming Tutorial: Structs and Mutating Functions

Arc Sosangyo
2 min readSep 7, 2021
Photo by: Patrick Baum at unsplash.com

This article is part of my Swift Programming Tutorial series.

Structs, short for structures, use properties and methods to facilitate more complex customization of data types. Properties are variables that works in the same way as storing values, while methods are functions containing a set of instructions that produce a specific outcome.

Here’s an example:

struct weapons {
var primary: String
var secondary: String
}

var aragorn: weapons = weapons(primary: "Sword", secondary: "Dagger")
var legolas: weapons = weapons(primary: "Bow", secondary: "Knives")

print(aragorn.primary)
print(legolas.secondary)

The output will be:

Sword
Knives

From the above example, var primary and var secondary are referred to as properties. Struct properties are essentially variables inside structs that represent values. By using structs, we can now easily require Aragorn and Legolas to have primary and secondary weapons. Doing this makes the code much more organized, which helps prevent data-type-related errors as your code becomes more sophisticated.

A function-like feature called a method can be declared in this way.

struct weapons {
var primary: String
var secondary…

--

--

Arc Sosangyo
Arc Sosangyo

Written by Arc Sosangyo

Arc is an iOS Dev and app publisher, a former IT manager who transitioned to iOS engineering, and a big fan of AI, coding, science, history, and philosophy.