Skip to content

Instantly share code, notes, and snippets.

@cpageler93
Created March 18, 2016 09:30
Show Gist options
  • Save cpageler93/dcb97b6aa63619302084 to your computer and use it in GitHub Desktop.
Save cpageler93/dcb97b6aa63619302084 to your computer and use it in GitHub Desktop.
Objective-C Singleton Pattern for Precompiler
//
// 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
//
// 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
#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