0
0
Swiftprogramming~5 mins

Classes are reference types in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that classes are reference types in Swift?
It means when you assign or pass a class instance, you are working with a reference to the same object in memory, not a copy. Changes affect the original instance.
Click to reveal answer
beginner
How does assigning one class instance to another variable affect the objects?
Both variables point to the same object. Changing the object through one variable changes it for the other too.
Click to reveal answer
intermediate
Compare classes and structs in Swift regarding value and reference types.
Classes are reference types (shared instance), structs are value types (copied on assignment).
Click to reveal answer
intermediate
What happens when you modify a property of a class instance passed to a function?
The original instance is modified because the function receives a reference to the same object.
Click to reveal answer
advanced
Why is understanding reference types important when working with classes?
Because it helps avoid unexpected bugs from shared data and helps manage memory and object behavior correctly.
Click to reveal answer
In Swift, what happens when you assign one class instance to another variable?
AThe original object is deleted.
BBoth variables refer to the same object.
CA new copy of the object is created.
DThe variables become constants.
Which of these is a reference type in Swift?
ATuple
BStruct
CEnum
DClass
If you change a property of a class instance inside a function, what happens outside the function?
AThe change is visible outside the function.
BThe change is lost after the function ends.
CA copy is changed, original stays the same.
DThe program crashes.
Why might using classes lead to unexpected bugs?
ABecause multiple variables can change the same object.
BBecause classes cannot have properties.
CBecause classes are slower than structs.
DBecause classes are immutable.
Which statement is true about structs compared to classes in Swift?
AStructs are slower than classes.
BStructs are reference types like classes.
CStructs are value types and copied on assignment.
DStructs cannot have methods.
Explain in your own words what it means that classes are reference types in Swift.
Think about what happens when you assign a class instance to a new variable.
You got /3 concepts.
    Describe the difference between how classes and structs behave when assigned to new variables.
    Consider what happens if you change a property after assignment.
    You got /3 concepts.