March 2017
Intermediate to advanced
226 pages
5h 13m
English
A singleton restricts type creation to a single instance. It limits access to shared resources and provides coordinated actions across its scope. Apple uses this pattern for a few limited types including UIApplication, NSProcessInfo, and NSWorkspace. Few design patterns are as hotly debated as singletons. You can answer the question of “Is it good or is it evil?” for yourself.
Singletons are safer and more approachable in Swift than in many other languages, as the following example demonstrates. It takes little work to build a restricted controller:
| | /// A singleton pattern struct with a single |
| | /// monotonically increasing state value |
| | public final class Singleton { |
| | // `default` or `shared` |
| | public ... |
Read now
Unlock full access