ZZomato·BehavioralL3DSA Round

Swift ARC and Reference Cycles

viaGlassdoor

Question: Explain classes vs structs in Swift (including how classes are maintained internally), dynamic dispatch, Automatic Reference Counting (ARC), and reference cycles.

Answer framing: Classes are reference types (heap-allocated, shared via pointers, support inheritance and dynamic dispatch through a vtable-like mechanism); structs are value types (stack-allocated where possible, copied on assignment, no inheritance). ARC automatically manages memory for class instances by tracking strong reference counts; when the count hits zero, the instance is deallocated. A reference cycle occurs when two objects hold strong references to each other, so neither's count ever reaches zero, causing a memory leak. Reference cycles are broken using weak or unowned references on one side of the relationship (e.g., delegate patterns typically use weak var delegate).

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account