Using didSet and private(set) on Swift Array

raoul picture raoul · Jul 2, 2015 · Viewed 8.3k times · Source

I'm working on a swift project and I have a couple of arrays. In one of my arrays, I do not want the client to be able to mutate it without using one of my specially-defined methods. On the other hand, I want the getter to be accessible. My questions comes up regarding append and setting properties.

Question 1: Does private(set) stop clients from calling array.append?

On another array I want to see if it has been changed.

Question 2: If I add a property observer onto the array using didSet , then is the didSet called when an element is appended to the array?

Answer

trevorj picture trevorj · Jul 2, 2015

Question 1: Does private(set) stop clients from calling array.append?

Yes it does.

Question 2: If I add a property observer onto the array using didSet , then is it called when an element is appended to the array?

Yes, didSet is called when append() is called on it.