What if your data could protect itself from accidental changes every time you share it?
Why Structs are value types (copy on assign) in Swift? - Purpose & Use Cases
Imagine you have a paper form filled with your details. You want to give a copy to a friend, but you also want to keep your original safe. If you just hand over the original, any changes your friend makes will affect your form too.
Manually copying data every time you assign or pass it around is slow and error-prone. You might forget to copy, or accidentally share the original, causing unexpected changes and bugs.
Structs in Swift automatically create a fresh copy when you assign or pass them. This means your original data stays safe and unchanged, while others work on their own copies without interference.
struct MyStruct {
var value: Int
}
var original = MyStruct(value: 0)
var copy = original // both share same data
copy.value = 10 // original also changesstruct MyStruct {
var value: Int
}
var original = MyStruct(value: 0)
var copy = original // copy is a new value
copy.value = 10 // original stays unchangedThis lets you work confidently with data, knowing changes won't sneakily affect other parts of your program.
Think of a game where each player has their own character stats. Using structs means each player's stats are independent copies, so one player's changes don't mess up another's.
Structs automatically copy data on assignment.
This prevents accidental changes to shared data.
It makes your code safer and easier to understand.
Practice
Solution
Step 1: Understand struct assignment behavior
Structs in Swift are value types, so assigning one struct to another copies the data.Step 2: Compare with reference types
Unlike classes, structs do not share references; each variable has its own copy.Final Answer:
A new copy of the struct data is created. -> Option AQuick Check:
Struct assignment = copy data [OK]
- Thinking structs share data like classes
- Assuming assignment links variables
- Confusing structs with reference types
Solution
Step 1: Recall Swift struct syntax
Structs are defined using the keywordstructfollowed by the name and braces containing properties.Step 2: Identify correct syntax
The correct syntax isstruct Person { var name: String }. Using=after the name is invalid, usingclassdefines a class instead of a struct, and adding parentheses likePerson()is incorrect.Final Answer:
struct Person { var name: String } -> Option BQuick Check:
Struct syntax = struct Name { properties } [OK]
- Using class keyword instead of struct
- Adding parentheses after struct name
- Using equals sign in struct definition
struct Point { var x: Int; var y: Int }
var p1 = Point(x: 5, y: 10)
var p2 = p1
p2.x = 20
print(p1.x)What is the output?
Solution
Step 1: Understand struct copying on assignment
Assigningp1top2copies the data, sop2is a separate copy.Step 2: Analyze mutation effect
Changingp2.xto 20 does not affectp1.x, which remains 5.Final Answer:
5 -> Option CQuick Check:
Struct copy means original unchanged [OK]
- Assuming p1.x changes to 20
- Confusing structs with reference types
- Expecting runtime error on assignment
struct Counter { var count: Int }
func increment(counter: Counter) {
var counter = counter
counter.count += 1
}
var c = Counter(count: 0)
increment(counter: c)
print(c.count)What is the output and why?
Solution
Step 1: Understand parameter passing for structs
Structs are passed by value, so the function receives a copy ofc.Step 2: Analyze mutation inside function
The function modifies its local copy, butcoutside remains unchanged.Final Answer:
0, because the function modifies a copy, not the original -> Option AQuick Check:
Structs passed by value = original unchanged [OK]
- Expecting original struct to change
- Thinking function parameter is reference
- Assuming compilation or runtime error
Solution
Step 1: Recall how to modify struct properties
Structs are value types; to modify properties inside methods, the method must be markedmutating.Step 2: Evaluate options
Use a mutating method inside the struct to change the property. correctly uses a mutating method. Use a non-mutating method and assign a new struct to self. is incorrect because non-mutating methods cannot change properties. Declare the struct as a class to allow property changes. changes the type to class, which is different. Use a global variable to hold the struct and update it. uses a global variable, which is not ideal.Final Answer:
Use a mutating method inside the struct to change the property. -> Option DQuick Check:
Mutating methods update struct properties [OK]
- Trying to modify properties in non-mutating methods
- Confusing structs with classes for mutability
- Using global variables instead of methods
Ready to prove it?
Pass the test and this pattern moves unknown
