Daily iOS
Memory and ARC

Shallow vs deep copy

In struct Order { var items: [Item]; let customer: Customer }, Item is a struct and Customer is a class. After var copy = order, what happens to the original if you change copy.customer.name?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

A struct copy copies the fields as they are, so a class property only copies the reference (a shallow copy) and the original's customer.name changes too. items is an array of structs, so it keeps value semantics.

Share a question