Value versus reference types, properties, initializers, protocols, extensions, enums with associated values and generics.
42 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a struct in Swift?
Show answer
A value type that groups related data and behavior together.
- What is a class in Swift?
Show answer
A reference type that defines a blueprint for creating objects with properties and methods.
- Are structs value types or reference types?
Show answer
Value types. Each copy has its own independent data.
- Are classes value types or reference types?
Show answer
Reference types. Multiple references point to the same object in memory.
- What happens when you assign a struct to another variable?
Show answer
A copy is made. Changes to one do not affect the other.
- What happens when you assign a class instance to another variable?
Show answer
The new variable references the same object. Both point to the same data.
- Where are struct instances stored in memory?
Show answer
On the stack.
- Where are class instances stored in memory?
Show answer
On the heap. The stack stores a reference to the object.
- When should you use a struct instead of a class?
Show answer
When you need a value type with no inheritance, simple data storage, or copy semantics.
- When should you use a class instead of a struct?
Show answer
When you need reference semantics, inheritance, or shared state across references.
- What is a stored property?
Show answer
A property that stores a value as part of the instance, like a variable.
- What is a computed property?
Show answer
A property that calculates its value each time it is accessed, defined with get or get/set.
- What does the willSet property observer do?
Show answer
It is called just before a stored property's value is set.
- What does the didSet property observer do?
Show answer
It is called immediately after a stored property's value is set.
- What is a lazy stored property?
Show answer
A property whose initial value is not computed until the first time it is accessed.