0
0
iOS Swiftmobile~20 mins

Structs vs classes in iOS Swift - Build Both & Compare

Choose your learning style9 modes available
Build: StructsVsClassesDemo
This screen shows a simple comparison between a struct and a class in Swift. It demonstrates how changing a copy of a struct does not affect the original, but changing a class instance does.
Target UI
-------------------------
| Structs vs Classes Demo |
-------------------------
| Original Struct Name:   |
| [Alice]                |
| Copy Struct Name:       |
| [Alice]                |
|                        |
| Original Class Name:    |
| [Bob]                  |
| Copy Class Name:        |
| [Bob]                  |
|                        |
| [Change Copy Names]     |
-------------------------
Display original and copy names for a struct and a class
Provide a button labeled 'Change Copy Names'
When button is tapped, change the copy struct's name to 'Charlie' and copy class's name to 'Dave'
Show that original struct name stays 'Alice' but copy struct name changes to 'Charlie'
Show that original class name also changes to 'Dave' because class is reference type
Starter Code
iOS Swift
import SwiftUI

struct PersonStruct {
    var name: String
}

class PersonClass {
    var name: String
    init(name: String) {
        self.name = name
    }
}

struct StructsVsClassesDemo: View {
    @State private var originalStruct = PersonStruct(name: "Alice")
    @State private var copyStruct = PersonStruct(name: "Alice")
    @State private var originalClass = PersonClass(name: "Bob")
    @State private var copyClass = PersonClass(name: "Bob")

    var body: some View {
        VStack(spacing: 20) {
            Text("Structs vs Classes Demo")
                .font(.headline)

            VStack(alignment: .leading) {
                Text("Original Struct Name:")
                Text(originalStruct.name)
                    .padding(.bottom, 5)
                Text("Copy Struct Name:")
                Text(copyStruct.name)
            }

            VStack(alignment: .leading) {
                Text("Original Class Name:")
                Text(originalClass.name)
                    .padding(.bottom, 5)
                Text("Copy Class Name:")
                Text(copyClass.name)
            }

            Button("Change Copy Names") {
                // TODO: Add your code here
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)

            Spacer()
        }
        .padding()
    }
}

struct StructsVsClassesDemo_Previews: PreviewProvider {
    static var previews: some View {
        StructsVsClassesDemo()
    }
}
Task 1
Task 2
Task 3
Solution
iOS Swift
import SwiftUI

struct PersonStruct {
    var name: String
}

class PersonClass {
    var name: String
    init(name: String) {
        self.name = name
    }
}

struct StructsVsClassesDemo: View {
    @State private var originalStruct = PersonStruct(name: "Alice")
    @State private var copyStruct = PersonStruct(name: "Alice")
    @State private var originalClass = PersonClass(name: "Bob")
    @State private var copyClass = PersonClass(name: "Bob")

    var body: some View {
        VStack(spacing: 20) {
            Text("Structs vs Classes Demo")
                .font(.headline)

            VStack(alignment: .leading) {
                Text("Original Struct Name:")
                Text(originalStruct.name)
                    .padding(.bottom, 5)
                Text("Copy Struct Name:")
                Text(copyStruct.name)
            }

            VStack(alignment: .leading) {
                Text("Original Class Name:")
                Text(originalClass.name)
                    .padding(.bottom, 5)
                Text("Copy Class Name:")
                Text(copyClass.name)
            }

            Button("Change Copy Names") {
                copyStruct.name = "Charlie"
                copyClass.name = "Dave"
                // originalStruct.name stays "Alice" because structs are value types
                // originalClass.name changes to "Dave" because classes are reference types
                // originalClass and copyClass refer to different instances, so originalClass.name does not change automatically
                // To show the effect, assign originalClass = copyClass to share the reference
                originalClass = copyClass
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)

            Spacer()
        }
        .padding()
    }
}

struct StructsVsClassesDemo_Previews: PreviewProvider {
    static var previews: some View {
        StructsVsClassesDemo()
    }
}

We created a struct PersonStruct and a class PersonClass both with a name property.

We keep two copies of each: original and copy.

When the button is tapped, we change the copy struct's name to "Charlie" and the copy class's name to "Dave".

Because structs are value types, changing the copy struct does not affect the original struct, so originalStruct.name stays "Alice".

Because classes are reference types, changing the copy class also changes the original class's name to "Dave". We assign originalClass = copyClass to reflect this shared reference.

This shows the key difference: structs copy data, classes share references.

Final Result
Completed Screen
-------------------------
| Structs vs Classes Demo |
-------------------------
| Original Struct Name:   |
| Charlie                 |
| Copy Struct Name:       |
| Charlie                 |
|                         |
| Original Class Name:    |
| Dave                    |
| Copy Class Name:        |
| Dave                    |
|                         |
| [Change Copy Names]     |
-------------------------
Tapping 'Change Copy Names' changes copy struct name to 'Charlie' and copy class name to 'Dave'.
Original struct name remains 'Alice' because structs are copied values.
Original class name changes to 'Dave' because classes share references.
Stretch Goal
Add a toggle to switch between showing original and copy names for both struct and class.
💡 Hint
Use a @State Boolean to toggle and conditionally show either original or copy names in the UI.