Singletons in Obj-C

Came across this pattern the other day:

    static dispatch_once_t onceToken;

    dispatch_once (&onceToken, ^{
        // Do some work that happens once
    });

This is the better way to implement a singleton in Obj-C. Reduces the code needed from the normal way of coding it out yourself, and guarantees that your object will only be instantiated once.

Some nice reading on the matter at bignerdranch and at stackoverflow

Comments