Skip to content

Instantly share code, notes, and snippets.

@virasio
Last active December 2, 2019 03:42

Revisions

  1. Victor Surikov revised this gist Feb 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Singleton.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    @@implementation MySingleton
    @implementation MySingleton

    // ...

  2. Victor Surikov revised this gist Feb 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Singleton.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    @implimentation MySingleton
    @@implementation MySingleton

    // ...

  3. 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;
    }

    // ...
  4. 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