Structs & Value Types

Structs are the backbone of Swift data modelling — Apple recommends them by default. By the end of this lesson you'll define struct s, understand value semantics (copy-on-assign), use the free memberwise initializer, and write mutating methods.

Learn Structs & Value Types in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Defining a Struct

Declare a struct and list its stored properties. Swift hands you a free memberwise initializer — one argument per property — so you rarely write init yourself.

2️⃣ Value Semantics (Copy on Assign)

This is the defining trait of structs: assigning one to a new variable, or passing it to a function, makes an independent copy . Changing the copy never touches the original — no hidden sharing, no surprises.

3️⃣ Mutating Methods

A method that changes the struct's own properties must be marked mutating . You can only call mutating methods on a var instance — a let struct is fully immutable.

Your turn. Fill in the blanks, then run it and check the output.

📋 Quick Reference

Build it, run it, and check your output against the example in the comments — especially the part proving copies are independent.

Practice quiz

What keyword defines a struct?

  • structure
  • class
  • struct
  • type

Answer: struct. Structs are declared with the struct keyword.

Are structs value types or reference types?

  • Value types
  • Reference types
  • Neither
  • Both at once

Answer: Value types. Structs are value types — they are copied on assignment.

When you assign a struct to a new variable, what happens?

  • Both share the same instance
  • The original is deleted
  • It becomes a reference
  • An independent COPY is made

Answer: An independent COPY is made. Value types are copied, so the two variables are independent.

What does Swift automatically generate for a struct's initializer?

  • Nothing
  • A memberwise initializer
  • A deinitializer
  • A copy constructor you must override

Answer: A memberwise initializer. Structs get a free memberwise initializer covering their stored properties.

Why must a method that changes a struct's properties be marked ?

  • Because struct instances are otherwise treated as immutable within methods
  • For speed
  • To make it static
  • It's optional styling

Answer: Because struct instances are otherwise treated as immutable within methods. mutating signals the method changes the value-type instance's properties.

Can you call a mutating method on a struct stored in a constant?

  • Yes
  • Only once
  • No — the instance is immutable
  • Only the first property

Answer: No — the instance is immutable. A let struct is immutable, so mutating methods are disallowed on it.

Given var a = Point(x:1,y:2); var b = a; b.x = 9 — what is a.x?

  • 9
  • 1
  • 0
  • nil

Answer: 1. b is a copy, so changing b.x leaves a.x unchanged at 1.

Do structs support inheritance from another struct?

  • Yes, fully
  • Only from classes
  • Only one level
  • No — structs cannot inherit from other structs

Answer: No — structs cannot inherit from other structs. Structs do not support inheritance; that's a class feature.

What is a good default choice in Swift for a simple data model?

  • Always a class
  • A struct (value type)
  • A tuple of 20 fields
  • A global variable

Answer: A struct (value type). Apple recommends preferring structs for most data models.

Can structs conform to protocols and have methods?

  • No
  • Only classes can
  • Yes — structs can have methods, computed properties, and conform to protocols
  • Only with inheritance

Answer: Yes — structs can have methods, computed properties, and conform to protocols. Structs support methods, computed properties, and protocol conformance.