Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-24-2009, 11:35 PM   #1 (permalink)
meowmix23F
Tutorial Author
 
Join Date: Jan 2009
Posts: 144
Default Singleton Classes

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

Code:
// 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]

etc.

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

Now, to our Singleton.m file

Code:
// 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];
}

@end
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)

Last edited by meowmix23F; 08-02-2009 at 11:55 AM.
meowmix23F is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,852
Threads: 89,216
Posts: 380,666
Top Poster: BrianSlick (7,129)
Welcome to our newest member, syrreintyia1
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.