Created
March 18, 2016 09:30
-
-
Save cpageler93/dcb97b6aa63619302084 to your computer and use it in GitHub Desktop.
Objective-C Singleton Pattern for Precompiler
This file contains 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
// | |
// ExampleUsageService.h | |
// ExampleUsage | |
// | |
// Created by Christoph Pageler on 18/03/16. | |
// Copyright © 2016 Christoph Pageler. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface ExampleUsageService : NSObject | |
SINGLETON_INTERFACE | |
@end |
This file contains 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
// | |
// ExampleUsageService.m | |
// ExampleUsage | |
// | |
// Created by Christoph Pageler on 18/03/16. | |
// Copyright © 2016 Christoph Pageler. All rights reserved. | |
// | |
#import "ExampleUsageService.h" | |
@implementation ExampleUsageService | |
SINGLETON_IMPLEMENTATION | |
@end |
This file contains 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
#define SINGLETON_INTERFACE \ | |
- (instancetype)init __attribute__((unavailable("use ´sharedInstance´ instead"))); \ | |
+ (instancetype)sharedInstance; \ | |
#define SINGLETON_IMPLEMENTATION \ | |
+ (instancetype)sharedInstance \ | |
{ \ | |
static id _sharedInstance = nil; \ | |
static dispatch_once_t onceToken; \ | |
dispatch_once(&onceToken, ^{ \ | |
_sharedInstance = [[[self class] alloc] init]; \ | |
}); \ | |
return _sharedInstance; \ | |
} \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment