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.

struct User {
}

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.

struct User {
    var name: String
    var age: 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.

let user = 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:

var user1 = User(name: "Alex", age: 25)
var user2 = 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.

struct User {
    var name: String
    var age: Int

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

Call the method like this:

let user = 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.

struct User {
    var name: String
    var age: Int

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

Now you create instances like this:

let user = 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.

struct Rectangle {
    var width: Double
    var height: Double

    var area: Double {
        return width * height
    }
}

Usage:

let rect = 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

struct Product {
    var title: String
    var price: Double

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

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

Example 2: Simple settings container

struct Settings {
    var isDarkMode: Bool
    var fontSize: Int
}

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

Structs allow mutation when declared with var.


Example 3: Type with mutation

struct Counter {
    var value: Int

    mutating func increment() {
        value += 1
    }
}

var counter = 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:

struct Counter {
    var value: Int

    func increment() {
        value += 1
    }
}

Why it breaks:

Struct methods cannot modify properties unless marked mutating.

Correct approach:

mutating func increment() {
    value += 1
}

Mistake 2: Expecting reference behavior

What you might do:

var user1 = User(name: "Alex", age: 25)
var user2 = 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.