Skip to content

Instantly share code, notes, and snippets.

@intari
Forked from virasio/Singleton.h
Created February 16, 2016 12:15

Revisions

  1. virasio revised this gist Aug 4, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Singleton.m
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,15 @@ + (instancetype) sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    sharedInstance = [[super alloc] initInstance];
    // Do any other initialisation stuff here
    });
    return sharedInstance;
    }

    - (instancetype) initInstance {
    return [super init];
    self = [super init];
    // Do any other initialisation stuff here
    // ...
    return self;
    }

    // ...
  2. virasio created this gist Apr 2, 2014.
    13 changes: 13 additions & 0 deletions Singleton.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    @interface MySingleton : NSObject

    // ...

    + (instancetype) sharedInstance;

    + (instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
    - (instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
    + (instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));

    // ...

    @end
    21 changes: 21 additions & 0 deletions Singleton.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    @implimentation MySingleton

    // ...

    + (instancetype) sharedInstance {
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    sharedInstance = [[super alloc] initInstance];
    // Do any other initialisation stuff here
    });
    return sharedInstance;
    }

    - (instancetype) initInstance {
    return [super init];
    }

    // ...

    @end