Structs are value types (copy on assign) in Swift - Time & Space Complexity
When working with structs in Swift, it's important to understand how copying affects performance.
We want to see how the cost changes when structs are assigned or passed around.
Analyze the time complexity of copying a struct with an array property.
struct DataStruct {
var numbers: [Int]
}
let n = 1000
var original = DataStruct(numbers: Array(1...n))
var copy = original // Copy happens here
copy.numbers.append(100)
This code creates a struct holding an array, then copies it and modifies the copy.
Look for operations that repeat or scale with input size.
- Primary operation: Copying the array inside the struct when modified.
- How many times: Happens once at the point of modification (append).
The copy of the array happens only when the copy is changed, not on assignment.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Copy about 10 elements |
| 100 | Copy about 100 elements |
| 1000 | Copy about 1000 elements |
Pattern observation: The cost grows linearly with the size of the array when modified.
Time Complexity: O(n)
This means copying a struct with an array costs time proportional to the array size when you change it.
[X] Wrong: "Assigning a struct is always a cheap operation regardless of size."
[OK] Correct: The actual copy of data happens only when you modify the copy, and that copy cost depends on the data size.
Understanding how structs copy data helps you explain performance in Swift code clearly and confidently.
"What if the struct contained only simple values instead of an array? How would the time complexity change?"
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
