开发者

Global NSDate variable

开发者 https://www.devze.com 2023-01-21 20:40 出处:网络
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-variab

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];


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号