Take the 2-minute tour ×

I'm aware of other posts on this topic but I'm only really one rung up the ladder from being a noob so need a bit more help.

My iPhone app has several global variables - some I have declared and given values in a class but others need to be set during a login process (like a token for example) that then need to be accessible for the lifecycle of the app from any class or method. I am told I should really be using a Singleton object for all of this which I presume is a class that's instantiated on startup. If so, could someone give me the simplest example of such header and implementation file and how/where I should instantiate it? Then I need to have some strings that are set from the off and others that can be set/got later on?

Thanks very much in advance. Also, I'm new here so if my etiquette is off in any way, please let me know.


share|improve this question
For singletons you just want to use Matt Gallagher's code that basically every iOS app uses: cocoawithlove.com/2008/11/… Here's the actual link to his macro file that is in almost every iOS app projectswithlove.com/projects/SynthesizeSingleton.h.zip Don't overuse singletons if you are a beginner. –  Joe Blow Mar 27 '11 at 13:23
add comment

2 Answers

up vote 3 down vote accepted

This link shows some code to create a singleton class : http://www.galloway.me.uk/tutorials/singleton-classes/

You would use it something like :

[[MyManager sharedManager] doSomething];

The call to sharedManager would get the one instance of the class (or, if this is the first time you called it, would create it) - this makes sure that you only have one of them :)

It also overrides release, retain, autorelease etc to make sure that you can't accidentally get rid of the sharedManager by mistake!

This class will instantiate itself the first time you use it but if you need it to be created on startup, just call [MyManager sharedManager] and it will create it for you.

You define the class like any other objective-c class - just add properties etc

Hope that helps :)

share|improve this answer
It's an option but only one I'd recommend if the op already understood how singletons worked! I much prefer knowing the mechanics before taking a shortcut. Though you're right - once you get the idea of a singleton, Mr Gallagher's code is awesome! –  deanWombourne Mar 29 '11 at 11:59
add comment

Global variables aren't good, but singletons aren't much better when they're just used to provide global access to some data. Anything bad you can say about a global variable, you can also say about a singleton that's used for global access. A better solution is to create a data model and pass that model from one view controller to the next.

Here's a previous SO question that might help.

share|improve this answer
This can be turned around. If singletons aren't much better than global variables, that implies that using global variables isn't much worse than creating singleton objects. That's partially true, but singleton objects do allow one to use Obj C properties to better encapsulate an app's public global state. –  hotpaw2 Mar 27 '11 at 16:28
I don't disagree. A singleton is marginally better than keeping a bunch of globals, but the reasons using globals is unwise are the same reasons that abusing singletons is unwise. –  Caleb Mar 27 '11 at 17:10
I prefer it the other way. If one knows that a small (not designed for reuse) app will require some singleton app or class-wide state, just use a few plain old C global variables. Which your answer potentially implies. –  hotpaw2 Mar 27 '11 at 17:18
You can get away with a lot in a small program, but that doesn't make it good practice. On the other hand, the work required to manage data properly in a small program is so small that some would say it's silly not to do it. Quick and dirty often turns out not to be all that quick. –  Caleb Mar 27 '11 at 17:27
A singleton allows you to defer instantiation and get slightly faster startup. However, pretty much ever app will have some sort of global state that has to be stored somewhere - some choose the delegate - I prefer singletons :) –  deanWombourne Mar 29 '11 at 11:57
add comment

Your Answer


