iOS Singletons – Objective-C


A singleton is a special kind of class where only one instance of the class exists for the current process. (In the case of an iOS app, the one instance is shared across the entire app.) Some examples in UIKit are [UIApplication sharedApplication] (which returns the sole instance of the application itself), and [NSFileManager defaultManager] (which returns the file manager instance). Singletons can be an easy way to share data and common methods across your entire app.

Rather than create instances of the singleton class using alloc/init, you’ll call a class method that will return the singleton object. You can name the class method anything, but common practice is to call it sharedName or defaultName.

Header

Implementation

Leave a comment

Your email address will not be published. Required fields are marked *