Reputation: 1133
Cocoa is full of singletons. Is there a logical/conventional difference between when the Cocoa APIs use
NSSingletonObject *so = [NSSingletonObject defaultSingleton];
versus
NSSingletonObject *so = [NSSingletonObject sharedSingleton];
?
Not a huge thing, but I don't really see why sometimes one is used versus the other.
Upvotes: 3
Views: 761
Reputation: 81288
I think it tends to be that if it is a true singleton (such as NSApplication) that you are using, then the -[JKFoo sharedFoo]
convention is followed. If on the other hand the class provides access to a default instance but you can still create other instances (for example NSNotificationQueue or NSFileManager) then the -[JKBar defaultBar]
convention is used.
Side note: if you are implementing a few of your own Cocoa singletons, then there is a useful OpenSource header you might want to take a look at :)
[edit: an even better singleton solution using GCD was pointed out by Mike Ash on his blog]
Upvotes: 7