0
0
Swiftprogramming~5 mins

Reference sharing and side effects in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does reference sharing mean in Swift?
Reference sharing means multiple variables point to the same instance of a class. Changing the instance through one variable affects all others pointing to it.
Click to reveal answer
beginner
What is a side effect in programming?
A side effect happens when a function or operation changes something outside its own scope, like modifying a shared object or variable.
Click to reveal answer
intermediate
How do struct and class differ in Swift regarding reference sharing?
Structs are value types, so each variable has its own copy. Classes are reference types, so variables share the same instance and changes affect all references.
Click to reveal answer
beginner
What happens when you modify a class instance through one reference in Swift?
All references to that instance see the change because they point to the same object in memory.
Click to reveal answer
intermediate
Why can reference sharing cause bugs in Swift programs?
Because changes through one reference affect others unexpectedly, leading to unintended side effects and harder-to-find bugs.
Click to reveal answer
In Swift, which type shares references by default?
AEnum
BClass
CStruct
DTuple
What is a side effect?
AChanging something outside the function's scope
BA function returning a value
CDeclaring a variable
DCalling a function
If two variables reference the same class instance and you change a property via one variable, what happens?
ABoth variables see the change
BOnly that variable sees the change
CThe change is ignored
DA new instance is created
Which Swift type avoids side effects by copying data on assignment?
AClass
BClosure
CProtocol
DStruct
Why is understanding reference sharing important?
ATo use more memory
BTo write faster code
CTo avoid unexpected changes and bugs
DTo avoid using classes
Explain reference sharing in Swift and how it can lead to side effects.
Think about how classes work compared to structs.
You got /4 concepts.
    Describe the difference between value types and reference types in Swift with examples.
    Consider what happens when you assign or pass these types.
    You got /4 concepts.