Reputation: 7707
The first approach that comes to my mind is to put the singleton object in the appDelegate object as property. In this way you can access it from anywhere using
#import "myAppDelegate.h"
// ...
[[(myAppDelegate *)[UIApplication sharedApplication] delegate] SingletonObj]
The downside is you have to explicitly cast and import the header of your delegate to tell the class you're working with that SingletonObj is actually a property of the delegate. And I think this makes the code smell a little.
The second approach is to create a legit singleton class. This, however, require more work. And I personally think that one Singleton class, is more than enough.
I'm not a programmer so I would greatly appreciate corrections on my reasoning, and opinions on the subject.
Upvotes: 1
Views: 235
Reputation: 52227
I found a xcode template for easy singleton generation on the net.
Upvotes: 1
Reputation: 18375
Best practice is to only ever put UIApplicationDelegate
-related things in your AppDelegate
class. Evarr.
Decide if you really need a singleton. If you do, just make a legit singleton class. It will be easier in the long run; did you notice how long it took to type that monster of a line, where you tried to get the singleton object out of the AppDelegate
? Agh.
(Also, just a little bit of idiom: classes start with a capital letter, and methods start with a lowercase letter; hence, you problem meant [[(MyAppDelegate *)[UIApplication sharedApplication] delegate] singletonObj]
.)
Upvotes: 0
Reputation: 162712
Singletons are most often handled in a class via a method like +sharedInstance
.
Upvotes: 5