Singleton in Objective-C/Swift

What is behind the Singleton?

Singleton: Instantiation of a class to ONLY one object

Features of Singleton:

  1. One class only can have only one instance;
  2. It must can create this instance automatically;
  3. This instance must be provided to other objects.

Benefits of Singleton:

All objects can only have one instance. In other words, all the instances, no matter how they were created, they have the same memory address. In practise, we want singleton class to manage our database read/write, file IO, to make sure the data integrity.

There are three ways to create an instance:

Option 1. using alloc and init
Option 2. using static method. A way such as [UIApplication sharedApplication] to create a new instance.
Option 3. copying from an existing object

For example, create a custom class named Item, and implement its singleton method as:

In the code:

  • Remember copyWithZone method must be override when you want to do the copy on object
  • allocWithZone is called when you create an instance by alloc and init
  • When you are using multi-threading, singleton instance should be avoided created multiple times.

Another way for you to create a singleton is using use GCD:

#import "Item.h"

static id _instance;

@implementation Item

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
    
    
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

@end

https://gist.github.com/arkilis/c3a51a4ef976ebda5c09cdfa33c5d6bd/

Updated: Singleton in Swift (2.0 and above)

class SingletonA {

    static let sharedInstance = SingletonA()

    init() {
        println("AAA");
    }

}

Reference

comments powered by Disqus