Skip to content

Instantly share code, notes, and snippets.

@yasirmturk
Created March 5, 2013 09:03
Show Gist options
  • Save yasirmturk/5088940 to your computer and use it in GitHub Desktop.
Save yasirmturk/5088940 to your computer and use it in GitHub Desktop.
Singleton classes is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere will return the UI…
//
// SingletonClass+ARC.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
#import <foundation/Foundation.h>
@interface SingletonClass : NSObject {
NSString *myProperty;
}
@property (nonatomic, retain) NSString *myProperty;
+ (id)sharedSingleton;
@end
//
// SingletonClass+ARC.m
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
#import "SingletonClass+ARC.h"
@implementation SingletonClass
@synthesize myProperty;
#pragma mark Singleton Methods
+ (id)sharedSingleton {
static SingletonClass *sharedMySingletonClass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMySingletonClass = [[self alloc] init];
});
return sharedMySingletonClass;
}
- (id)init {
if (self = [super init]) {
myProperty = @"Default Value";
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment