Created
February 6, 2014 17:28
-
-
Save fjcaetano/8848776 to your computer and use it in GitHub Desktop.
objc-singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FJCSingleton.h | |
// Singleton | |
// | |
// Created by Flávio Caetano on 2/6/14. | |
// Copyright (c) 2014 flaviocaetano.com. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface FJCSingleton : NSObject | |
+ (instancetype)sharedInstance; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FJCSingleton.m | |
// Singleton | |
// | |
// Created by Flávio Caetano on 2/6/14. | |
// Copyright (c) 2014 flaviocaetano.com. All rights reserved. | |
// | |
#import "FJCSingleton.h" | |
@interface FJCSingleton() | |
- (instancetype)initSuper; | |
@end | |
@implementation FJCSingleton | |
- (id)init | |
{ | |
[NSException raise:@"Singleton Class" format:@"You must use [%@ sharedInstance] instead", NSStringFromClass(self.class)]; | |
return nil; | |
} | |
- (instancetype)initSuper | |
{ | |
if(self = [super init]) | |
{ | |
<#Init Code#> | |
} | |
return self; | |
} | |
+(instancetype)sharedInstance | |
{ | |
__block FJCSingleton *sharedInstance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedInstance = [[FJCSingleton alloc] initSuper]; | |
}); | |
return sharedInstance; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment