tags:

views:

24

answers:

1

I'm trying to use an NSDate variable captured in another method. After much reading and searching I thought I found the answer on this site: http://www.everydayone.com/2009/08/nsdate-as-global-variable/. I first tried declaring the variables in the AppDelegate.h as specified in the article. The result was 2 failures in theViewController implementation file: both firstDate and startDate undeclared. I then tried declaring them in the ViewController.h which allowed the code to compile without errors. However, when the method runs as shown below the app crashes and I get the following message "GDB: Program received signal: EXC_BAD_ACCESS". If I uncomment the "NSDate *today" line and use that variable instead of secondDate the code works fine. Your help would be greatly appreciated.

.h:

@interface DatesViewController : UIViewController { 
    NSDate      *firstDate;
    NSDate      *secondDate;
}

@property (nonatomic, retain) NSDate *firstDate;
@property (nonatomic, retain) NSDate *secondDate;

.m:

@synthesize firstDate;
@synthesize secondDate;

-(IBAction)getFirstDate:(id)sender{
    firstDate = [picker date];
}


-(IBAction)getSecondDate:(id)sender{
    secondDate = [picker date];
    //NSDate *today = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSTimeInterval interval = [firstDate timeIntervalSinceDate: secondDate];
A: 

Try referring to firstDate and secondDate as

self.firstDate;
self.secondDate;

Also, make sure there's no way for getSecondDate() to be called before getFirstDate(); you aren't checking if firstDate exists or is initialized in getSecondDate()

Sam Dufel