Explain the primary differences between a struct and a class in Swift
Tests value versus reference semantics and their impact on memory and mutation. Strong answers note structs copy on assignment and lack inheritance, while classes share heap instances via ARC. Red flag: claiming structs are always stack-allocated.
Tests whether you understand value versus reference semantics and how that shapes memory layout, mutation behavior, and architectural choices. A strong answer covers four things in order: first, structs copy on assignment while classes share a single heap instance; second, structs lack inheritance and deinit; third, classes incur ARC overhead and retain cycles; fourth, copy-on-write enables efficient large struct mutation. Red flag: claiming structs are always on the stack, that classes are faster, or that value types cannot leak.
Read the original → docs.swift.org
- #swift
- #memory-management
- #value-types
- #reference-types
- #ios
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.