Hacker News new | ask | show | jobs
by hoopadoop 5455 days ago
Where does IOS use Singletons?
3 comments

Depends on your definition of singleton.

In java, singletons are enforced by the type system - i.e. There's no public way to make more of the object. Usually that's what people mean by 'singleton' in java. If you don't have the source and you outgrow the single instance, there's nothing you can do except stop using the class.

In Objective-C, people do occasionally simulate that by mungng init and alloc, but that is definitely a bad idea. More often a singleton class will have a conventional init and alloc, but have a class method like +sharedInstance which lazily instantiates and dispenses a single instance of the object during the lifetime of the application. That's what people most often mean by 'singleton' in objective-c. It's basically a global variable, but when you outgrow it, it's often not hard to switch to allocating more than one.

Also, partly due to what many would see as a deficiency - the fact that it's much harder to produce architecture independent libraries in objective-c than java, the source is available more of the time, so evolving past a singleton is more often straightforward.

any method that has 'shared' or 'current' in front.

http://developer.apple.com/library/ios/#documentation/uikit/...

It is almost everwhere.

[MyClass sharedMyclass] is the Objective-C equivalent of getInstance()

Also, singletons make good sense in mobile apps, as it is a very different type of a beast than a server side app.