Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Singleton Classes

meowmix23Fmeowmix23F Posts: 144Tutorial Authors
edited January 2012 in iPhone SDK Tutorials
Have you ever wanted to share data between views, but couldn't figure it out for the life of you? Now, here's a solution. A singleton class. Any class can generate a reference (pointer) to the shared version of the class, which is only allocated once. You can then set values and retreive them, or write up any class methods you'd like.

Basically, instead of calling

myclass *instance = [[myclass alloc] init];

every time you want to reference a class (and bear in mind, it's a *new* instance of that class, so if you add variables to it the next time you allocate one of those classes it won't have the variables)

You'd call

myclass *instance = [myclass sharedInstance];

Then, any modifications to that instance change the entire thing, meaning that should you get another reference to +sharedInstance later it would still have the variables from before, because, in essence, it's the same instance! Should you want to share data, all you'd have to do is add a variable to the singleton's interface, such as an NSString.

NSString *myVariable

Then, you'd declare properties for it and synthesize it as usuaul.

From your other class, you'd include the header file for the singleton class (#import "singleton.h")
And then you'd just get a reference to the shared instance (using my example method below to create the instance),
and then change variables. Then, from another class, you can get a reference to the shared instance and look at what those variables are.

Here's a small example
// Singleton.h

@interface Singleton : NSObject 

NSMutableDictionary *keys;

@property (nonatomic, retain) NSMutableDictionary *keys;
+ (Singleton *)sharedSingleton;

As you can see, we have one "class" method called "sharedSingleton". It is a class method because it starts with a plus sign, instead of the usual minus sign. One thing about class methods is that they normally return autoreleased objects. In iPhone programming you've seen this in:

[NSUserDefaults standardUserDefaults]
[UIDevice currentDevice]
[UIScreen mainScreen]


If you make a pointer to one of them, it's automatically released (autorelease!)

Now, to our Singleton.m file
// singleton.m

static Singleton *shared = NULL;

@implementation Singleton

- (id)init
if ( self = [super init] )
self.keys = [[NSMutableDictionary alloc] init];
return self;


+ (Singleton *)sharedSingleton
@synchronize shared
if ( !shared || shared == NULL )
// allocate the shared instance, because it hasn't been done yet
shared = [[Singleton alloc] init];

return shared;

- (void)dealloc
NSLog(@"Deallocating singleton...");
[keys release];

[super dealloc];


As you can see, we allocate a new "sharedInstance" if the current one is null.

Now, we just expose the keys as a property, and you basically have your own NSUserDefaults (a way of interacting between views).

Say, you have a variable from one view that you want to pass to another.

ie: score = @"500";

in the first view, you'd have to import singleton.h

then you'd say

Singleton *singleton = [Singleton sharedSingleton];
[singleton.keys setObject:score forKey:@"score"];

in your other view, you'd do this, only you'd use objectForKey on the keys property

anybody, feel free to post and correct something, as i've only used singleton's a few times (well custom ones atleast)
Post edited by meowmix23F on


Sign In or Register to comment.