Objective-C (iOS): Singleton

ARC 用シングルトン

Singleton.h

#import <Foundation/Foundation.h>

@interface Singleton : NSObject

+ (Singleton *)sharedManager;

@end

Singleton.m

#import "Singleton.h"

@implementation Singleton

+ (Singleton *)sharedManager {
    static Singleton *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
    if (self = [super init]) {
        // write something here.
    }
    return self;
}

@end