How to Create a Struct in Swift

What you’ll build or solve

You’ll create custom data types using struct and use them in functions and apps.

When this approach works best

Creating a struct works best when:

  • You model simple data like a user, product, or settings object.
  • You want value semantics, where copies are independent.
  • You group related properties into one clean type.
  • You build lightweight data models for SwiftUI or networking.

This is a bad idea when you need inheritance or shared reference behavior. In those cases, use a class instead.

Prerequisites

  • Xcode installed
  • Basic Swift syntax
  • Understanding of variables and functions

Step-by-step instructions

Step 1: Declare a struct

Use the struct keyword followed by a name.

structUser {
}

This creates a new type named User.

By convention, struct names start with a capital letter.


Step 2: Add properties

Add stored properties inside the struct.

structUser {
varname:String
varage:Int
}

Each property defines part of the struct’s data.

Swift automatically creates a memberwise initializer for you.


Step 3: Create an instance

Create an instance using the default initializer.

letuser=User(name:"Alex",age:25)
print(user.name)

Each instance holds its own copy of the data.

Since structs are value types, assigning them creates a copy:

varuser1=User(name:"Alex",age:25)
varuser2=user1

user2.name="Sam"

print(user1.name)// Alex
print(user2.name)// Sam

Changes to user2 do not affect user1.


Step 4: Add methods

Structs can contain functions.

JavaScript

structUser {
varname:String
varage:Int

funcgreet() {
print("Hello, I am \(name)")
    }
}

Call the method like this:

letuser=User(name:"Alex",age:25)
user.greet()

Methods can access the struct’s properties directly.


Step 5: Add a custom initializer

If you need special setup logic, write your own initializer.

structUser {
varname:String
varage:Int

init(name:String) {
self.name=name
self.age=0
    }
}

Now you create instances like this:

letuser=User(name:"Taylor")

When you define a custom initializer, Swift no longer provides the default memberwise initializer automatically.

What to look for

  • Use self to refer to properties.
  • All stored properties must be initialized.
  • If you define a custom initializer, you may need to add others manually.

Step 6: Add computed properties

Structs can include computed properties.

structRectangle {
varwidth:Double
varheight:Double

vararea:Double {
returnwidth*height
    }
}

Usage:

letrect=Rectangle(width:5,height:4)
print(rect.area)

area is calculated dynamically and does not store a value.


Examples you can copy

Example 1: Product model

structProduct {
vartitle:String
varprice:Double

funcformattedPrice()->String {
return"$\(price)"
    }
}

letproduct=Product(title:"Laptop",price:999.99)
print(product.formattedPrice())

Example 2: Simple settings container

structSettings {
varisDarkMode:Bool
varfontSize:Int
}

varsettings=Settings(isDarkMode:true,fontSize:16)
settings.fontSize=18

Structs allow mutation when declared with var.


Example 3: Type with mutation

structCounter {
varvalue:Int

mutatingfuncincrement() {
value+=1
    }
}

varcounter=Counter(value:0)
counter.increment()
print(counter.value)

Use the mutating keyword when a method modifies properties.


Common mistakes and how to fix them

Mistake 1: Forgetting mutating in modifying methods

What you might do:

structCounter {
varvalue:Int

funcincrement() {
value+=1
    }
}

Why it breaks:

Struct methods cannot modify properties unless marked mutating.

Correct approach:

JavaScript

mutatingfuncincrement() {
value+=1
}

Mistake 2: Expecting reference behavior

What you might do:

varuser1=User(name:"Alex",age:25)
varuser2=user1

user2.age=30

Why it surprises you:

Structs are value types. user2 is a copy.

Correct approach:

Use a class if you need shared references.


Troubleshooting

If Swift says a property is not initialized, check that all stored properties are set in the initializer.

If a method cannot modify a property, add the mutating keyword.

If changes do not reflect across variables, remember that structs are copied.

If you cannot access a property, confirm it is declared inside the struct and not private.


Quick recap

  • Use struct to define a custom value type.
  • Add stored properties inside the braces.
  • Swift provides a memberwise initializer automatically.
  • Add methods to define behavior.
  • Use mutating for methods that modify properties.
  • Structs use value semantics, not reference semantics.