1

I have to use a NSDate var in my program, and that var will be dealloc and realloc (I must add some month and year to this date and there is no other possibilities).

That var has to be user in many methods and I want to put this var in global. Is there any other options? It's not clean but I don't know how to do in an other way...

Thanks a lot to help me !!!

clement
  • 4,204
  • 10
  • 65
  • 133

3 Answers3

1

I would recommend to put it into your AppDelegate. Then you can get it via

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [appDelegate myGlobalDate]);

Of course then you need getter and setter for myGlobalDate in MyAppDelegate.

dasdom
  • 13,975
  • 2
  • 47
  • 58
  • 1
    I vote against this approach. Don't let the app delegate be the "master super doeverything manager" class. Been there, done that, learnt from it. :) – Eiko Apr 28 '11 at 15:43
  • 1
    Do not clutter your app-delegate with such nonsense. – Till Apr 28 '11 at 15:44
  • 2
    Ok, then please let us know what you learned from this mistake. – dasdom Apr 28 '11 at 15:45
  • OK, THANK YOU, I will scratch my head monday with that approach ;-) – clement Apr 28 '11 at 15:47
  • 1
    @dasdom I learnt that it's very easy to push every oh-so-small piece conveniently into the app delegate, degrading code quality step by step. And once you have that big stinking class, it get's *very* difficult to pull it straight again. The app delegate should really just do nothing else than managing the app lifecycle. (Ok, if that particular property belongs into that category, it's fine there.) – Eiko Apr 28 '11 at 18:40
  • Thank you again. I read about it after your post and I am going to check my current app delegate tomorrow. – dasdom Apr 28 '11 at 20:30
  • I don't saw that we can save a NSDate as a sharedApplication, so I made a @protected in the interface of the class. – clement May 02 '11 at 08:57
  • No, you misunderstood my answer. I meant you can save the NSDate IN the sharedApplication. But you should think about the better implementations mentioned by Eiko and pohl. – dasdom May 02 '11 at 09:19
  • I've put it in a @protected in the @interface. She's visible in the viewDidLoad but not in my custom method (that deal my swipe gesture)... I'm not able to understand it for the moment :/ – clement May 02 '11 at 11:43
1

Think about what purpose the variable has, and where you use it most often. Then you should have found a natural place for it.

Global variables are not something absolutely terrible, nor are singletons (which might be a good fit here). But then, maybe it really belongs to the user defaults, or a certain view controller.

Eiko
  • 25,601
  • 15
  • 56
  • 71
1

In answer to the question about whether there are other options (and not speaking to the point of whether one should do this). One option is to make a class specifically as a place to keep variables that you need to have available globally. One example from this blog post

@interface VariableStore : NSObject
{
    // Place any "global" variables here
}
// message from which our instance is obtained
+ (VariableStore *)sharedInstance;
@end

@implementation VariableStore
+ (VariableStore *)sharedInstance
{
    // the instance of this class is stored here
    static VariableStore *myInstance = nil;

    // check to see if an instance already exists
    if (nil == myInstance) {
        myInstance  = [[[self class] alloc] init];
        // initialize variables here
    }
    // return the instance of this class
    return myInstance;
}
@end

Then, from elsewhere:

[[VariableStore sharedInstance] variableName]

Of course, if you don't like the way they instantiate the singleton in the above example, you can choose your favorite pattern from here. I like the dispatch_once pattern, myself.

Community
  • 1
  • 1
pohl
  • 3,158
  • 1
  • 30
  • 48