Tuesday, February 22, 2022

What will happen when we change singleton class to structure?

Only classes can be singletons in Swift.

Structures are value types, and copies are created every time, you pass a value to a function or a type. So due to this it will create multiple copies. But according to Singleton design pattern, there should be only one global instance for a class.

This clearly defeats the purpose of singletons.


  

No comments:

Post a Comment