SwiftMidMCQ
What is the difference between `struct` and `class` in Swift?
Test your knowledge:
Explanation & Code
Answer: The core difference is value type vs reference type.
struct | class | |
|---|---|---|
| Type | Value type | Reference type |
| Memory | Stack (usually) | Heap |
| Inheritance | No | Yes |
| Mutability | Explicit (mutating) | Implicit |
| ARC | No | Yes |
deinit | No | Yes |
Rule of thumb:
- Use
structby default — safer, faster, no retain cycles - Use
classwhen you need identity, inheritance, or Objective-C interop
Rate your understanding: