全局NSDate变量

时间:2010-10-26 14:48:46

标签: iphone nsdate

我正在尝试使用另一种方法捕获的NSDate变量。经过多次阅读和搜索,我以为我在这个网站上找到了答案:http://www.everydayone.com/2009/08/nsdate-as-global-variable/。我首先尝试按照文章中的规定声明AppDelegate.h中的变量。结果是在ViewController实现文件中有2个失败:firstDate和startDate都未声明。然后我尝试在ViewController.h中声明它们,这允许代码编译而没有错误。但是,当方法运行如下所示时,应用程序崩溃,我收到以下消息“GDB:程序接收信号:EXC_BAD_ACCESS”。如果我取消注释“NSDate * today”行并使用该变量而不是secondDate,则代码可以正常工作。非常感谢您的帮助。

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

1 个答案:

答案 0 :(得分:0)

尝试将firstDate和secondDate称为

self.firstDate;
self.secondDate;

另外,确保在getFirstDate()之前无法调用getSecondDate();您没有检查firstDate是否存在或是否在getSecondDate()

中初始化