Any reason not use use a singleton “variable” in Swift?

Functionally, these are very similar, but I’d advise using the Model.shared syntax because that makes it absolutely clear, wherever you use it, that you’re dealing with a singleton, whereas if you just have that model global floating out there, it’s not clear what you’re dealing with.

Also, with globals (esp with simple name like “model”), you risk of having some future class that has similarly named variables and accidentally reference the wrong one.

For a discussion about the general considerations regarding globals v singletons v other patterns, see Global Variables Are Bad which, despite the fairly leading title, presents a sober discussion, has some interesting links and presents alternatives.


By the way, for your “OCD friends” (within which I guess I must count myself, because I think it’s best practice), not only would declare init to be private, but you’d probably declare the whole class to be final, to avoid subclassing (at which point it becomes ambiguous to what shared references).

Leave a Comment