From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Read-only singletons

Read-only singletons - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Read-only singletons

- [Instructor] Let's implement a Singleton design pattern in Swift. I've gone ahead and created an iOS application called AppSettingsDemo using the single view template. We're going to implement a central repository for accessing application settings from any part of the application. This is a perfect candidate for the Singleton since creating more than one object of this type would be a waste of resources. We start by adding the new file called AppSettings. In the Project Navigator, I choose add New File. It should be a Swift file, and I call it AppSettings. Next, I declare a class called AppSettings. I make it final to prevent the creation of subclasses. We've got the final public class, and now, let's create the dictionary that holds the application settings. Normally, you'd use a property list or user defaults to store the settings, but let's keep it simple for this demo and focus on the Singleton instead. So I'll declare the dictionary is private, so variable, and I call it…

Contents