Reference sharing means two or more variables point to the same data. Side effects happen when changing data through one variable also changes it for others.
Reference sharing and side effects in Swift
class MyClass { var value: Int init(value: Int) { self.value = value } } let a = MyClass(value: 10) let b = a // b references the same object as a b.value = 20 // changes value inside the shared object
Classes in Swift are reference types, so variables hold references to the same object.
Changing the object through one variable affects all variables referencing it.
class Box { var content: String init(content: String) { self.content = content } } let box1 = Box(content: "Apple") let box2 = box1 box2.content = "Orange" print(box1.content) // Output: Orange
struct Point { var x: Int var y: Int } var p1 = Point(x: 1, y: 2) var p2 = p1 p2.x = 10 print(p1.x) // Output: 1
counterA and counterB both reference the same Counter object. Changing count via counterB also changes it for counterA.
class Counter { var count: Int init(count: Int) { self.count = count } } let counterA = Counter(count: 5) let counterB = counterA print("Before change:") print("counterA.count = \(counterA.count)") print("counterB.count = \(counterB.count)") counterB.count += 10 print("After change:") print("counterA.count = \(counterA.count)") print("counterB.count = \(counterB.count)")
Reference sharing happens only with classes (reference types), not structs or enums (value types).
Be careful: changing shared data can cause unexpected bugs if you forget about other references.
Use 'copy' or create new instances if you want independent data.
Classes in Swift share references, so variables can point to the same object.
Changing data through one reference affects all others pointing to that object.
Structs do not share references; they copy data instead.
Practice
Solution
Step 1: Understand class assignment behavior
In Swift, classes are reference types, so assigning one instance to another variable makes both variables point to the same object.Step 2: Effect of modifying through one variable
Changing the object through one variable affects the object seen by the other variable because they share the same reference.Final Answer:
Both variables refer to the same object; changes affect both. -> Option CQuick Check:
Class assignment shares references = D [OK]
- Thinking classes copy data on assignment
- Confusing classes with structs
- Assuming changes affect only one variable
Solution
Step 1: Check correct syntax for class instance creation
In Swift, you create a class instance with MyClass() and assign it to a variable using let or var with an equals sign.Step 2: Verify assignment to another variable
Assigning obj1 to obj2 is done simply by let obj2 = obj1; no parentheses or 'new' keyword are used.Final Answer:
let obj1 = MyClass(); let obj2 = obj1 -> Option AQuick Check:
Correct class instance assignment syntax = A [OK]
- Using 'new' keyword like other languages
- Adding parentheses after variable names
- Missing semicolons but Swift allows them optionally
class Box {
var value: Int
init(value: Int) { self.value = value }
}
let box1 = Box(value: 10)
let box2 = box1
box2.value = 20
print(box1.value)What is the output?
Solution
Step 1: Understand reference sharing in class instances
box1 and box2 both refer to the same Box object because classes are reference types.Step 2: Effect of changing box2.value
Changing box2.value to 20 changes the single shared object, so box1.value also reflects 20.Final Answer:
20 -> Option AQuick Check:
Class reference sharing means box1.value = 20 [OK]
- Assuming box1.value stays 10 after box2 changes it
- Confusing class with struct behavior
- Expecting a compile or runtime error
class Person {
var name: String
init(name: String) { self.name = name }
}
var p1 = Person(name: "Alice")
var p2 = p1
p2.name = "Bob"
print(p1.name)Solution
Step 1: Analyze reference sharing in the code
p1 and p2 both point to the same Person instance because Person is a class.Step 2: Effect of changing p2.name
Changing p2.name to "Bob" changes the shared object, so p1.name also becomes "Bob".Final Answer:
Nothing is wrong; output is "Bob". -> Option BQuick Check:
Class reference sharing means p1.name = "Bob" [OK]
- Expecting p1.name to remain "Alice"
- Thinking var vs let affects reference sharing
- Believing class must be struct for copying
Solution
Step 1: Understand copying behavior of classes
Classes in Swift do not copy on assignment; variables share references by default.Step 2: Use NSCopying protocol for explicit copying
To create a true copy, the class must conform to NSCopying and implement copy(with:) method, then call copy() to get a new instance.Step 3: Evaluate other options
Assigning to a new variable shares the reference. Declaring as a struct changes the type but is not always possible. The 'clone' keyword does not exist in Swift.Final Answer:
Make the class conform to NSCopying and implement copy(with:), then use copy(). -> Option DQuick Check:
Explicit copy via NSCopying = C [OK]
- Assuming assignment copies the object
- Confusing structs with classes for copying
- Expecting a 'clone' keyword in Swift
Ready to prove it?
Pass the test and this pattern moves unknown
