Daily iOS
Swift

Protecting the original

You passed a class instance to a function, changed a property inside it, and the caller's original changed too. How do you protect the original?

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 class is a reference type, so inside and outside the function see one object. To protect the original, model it as a value type like a struct, make a copy before passing it, or expose it through a read-only protocol.

Share a question