Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: NSDate value set in one method, is inaccessible from another
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDate value set in one method, is inaccessible from another



Hi Remy, 

1. I didn't know Objective-C didn't have class-level variables! Thanks for pointing it out!

2. Yes, I've set the project (in Xcode) to do ARC (so, I believe that should take care).

3. Here is the code:


In ViewController.h
....
....

@property (nonatomic, retain) NSDate *historyFromDate;

@property (nonatomic, retain) NSDate *historyToDate;

....
....

-(IBAction) fromDateChosen: (id)sender;

-(void) fetchTheHistory;

-(void) setDates: (NSDate *)date

        withType:(int) type;




In ViewController.m

...
...

@synthesize historyFromDate;

@synthesize historyToDate;

....
....


-(IBAction) fromDateChosen: (id)sender {

    

    NSString *buttonTitle = @"I've chosen the 'FROM' date";


    if ([[buttonDateChosen currentTitle] isEqualToString:buttonTitle]) { 


        NSLog(@"User has chosen the 'From' date");

    

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];

    

        // Get the chosen date value

        NSDate *fromDate = [datePicker date];

        historyFromDate = fromDate;

        

        

        // Set the 'to' date label to reflect the user's choice

        labelFromDate.text = [dateFormatter stringFromDate:historyFromDate];

        NSLog(@"'From' Date Chosen:%@", historyFromDate);

              //[dateFormatter stringFromDate:[datePicker date]]);


        [self fetchTheMoodHistory];

    }

}

...

...

...


-(void) fetchTheHistory {


    NSLog(@"Calling fetchTheHistory for the period from %@", historyFromDate);

    ...

    ...

}


...
...


fromDateChosen gets called after the user chooses a date form a Date Picker object in the UI.
Within the method 'fromDateChosen', when I print the historyFromDate, the value is correct. 
But, when I print it in fetchTheHistory method, the value shows the current date/time (not the one the user chose). 







On Thu, Apr 19, 2012 at 11:24 PM, Remy Demarest <email@hidden> wrote:
You have to understand first that "class-level" variables don't exist, if you're defining the variable outside of an ivar block or a method it will be a global, this is evil btw.

Now, if you're not using ARC (Automatic Reference Counting), then your date will be deallocated, trying to accessing it will result in a crash, or just display a broken value if you're lucky.

I suggest to use a static variable if you really want a class-level variable, and I suggest making class-accessors. In any case the code you provide isn't really enough to provide a proper diagnostic, could you send your class implementation with the method implementations?

Remy "Psy" Demarest

Le 19 avr. 2012 à 13:44, The Rhythmic a écrit :

Am sorry to ask such a trivial question. Am a newbie to Objective-C, & simply cannot see how to get this working, after having tried several possible ways & google'd around for it. Please help!
My question is simple. I have a class-level NSDate object, which is declared outside any method in the class as:

    NSDate *fromDate;

Now, within a method, am setting this value to the date from a DatePicker as:

    fromDate = [datePicker date];

Soon after the above assignment, I print its value into the log & it works fine.

    NSLog(@"From Date: %@", fromDate);
 
Now, when I use NSDate's value in another/different method, the value's gone! Why is it not persisted across methods in the same class itself? What can I do for the value to be accessible across methods? 
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)

This email sent to email@hidden


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >NSDate value set in one method, is inaccessible from another (From: The Rhythmic <email@hidden>)
 >Re: NSDate value set in one method, is inaccessible from another (From: Remy Demarest <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.