0
0
Swiftprogramming~5 mins

Structs are value types (copy on assign) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that structs are value types in Swift?
It means when you assign a struct to a new variable or pass it to a function, Swift makes a copy of the struct instead of sharing the same instance.
Click to reveal answer
beginner
How does assigning one struct variable to another affect their data?
Each variable gets its own separate copy of the data. Changing one does not affect the other.
Click to reveal answer
intermediate
Compare structs and classes in Swift regarding assignment behavior.
Structs are value types and are copied on assignment. Classes are reference types and share the same instance when assigned.
Click to reveal answer
intermediate
Why might Swift use value types like structs for some data?
Value types help avoid unexpected changes because each copy is independent. This makes code safer and easier to understand.
Click to reveal answer
beginner
If you change a property of a struct after assigning it to another variable, what happens?
Only the struct you changed is affected. The other variable still has the original data because it has its own copy.
Click to reveal answer
What happens when you assign a Swift struct to a new variable?
AA new copy of the struct is created.
BBoth variables point to the same struct instance.
CThe struct is converted to a class automatically.
DThe original struct is deleted.
If you modify a struct after assigning it to another variable, what happens to the other variable?
AIt stays the same because each variable has its own copy.
BIt also changes because they share the same data.
CIt becomes nil automatically.
DIt throws a runtime error.
Which of these is a reference type in Swift?
AEnum
BStruct
CClass
DTuple
Why are structs considered safer for some data than classes?
ABecause they use less memory.
BBecause they are copied on assignment, avoiding shared state bugs.
CBecause they can inherit from other structs.
DBecause they run faster than classes.
Which keyword defines a struct in Swift?
Aclass
Benum
Cvalue
Dstruct
Explain in your own words what it means that structs are value types and how assignment works.
Think about what happens when you give a copy of a drawing to a friend.
You got /4 concepts.
    Describe the difference between structs and classes in Swift regarding how they handle data when assigned to new variables.
    Imagine copying a photo versus sharing a link to the photo.
    You got /4 concepts.