我希望两次得到区别。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setCurrentQuestion];
}
-(void)setCurrentQuestion{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
startTime = [dateFormatter stringFromDate:[NSDate date]];
date1 = [dateFormatter dateFromString:startTime];
[self answerTapped];
}
-(void)answerTapped{
endtime = [dateFormatter stringFromDate:[NSDate date]];
date2 = [dateFormatter dateFromString:endtime];
[dateFormatter release];
dateFormatter = nil;
NSTimeInterval * timeDifference =[date2 timeIntervalSinceDate:date1];
}
我正在使用此代码来获取开始时间和结束时间之间的差异,但我收到的错误是" Inintializing' NSTimeInterval *' (又名' double ')表达了不兼容的类型' NSTimeInterval' (又名' double ')" at" NSTimeInterval * timeDifference = [date2 timeIntervalSinceDate:date1]; "线。这个错误意味着什么?怎么解决这个?或者请告诉我如何获得时差?请帮忙。
答案 0 :(得分:4)
它是星号。您将timeDifference声明为指向NSTimeInterval的指针。正确指出它只是一个双倍。只需改为:
NSTimeInterval timeDifference =[date2 timeIntervalSinceDate:date1];
另外,我认为您的日期和字符串过早发布。最简单的方法是声明日期的属性。对于预ARC:
@property (retain, nonatomic) NSDate * date1;
@property (retain, nonatomic) NSDate * date2;
或ARC:
@property (strong, nonatomic) NSDate * date1;
@property (strong, nonatomic) NSDate * date2;
然后用这样的点运算符赋值:
self.date1 = [NSDate date];
根本不需要这些字符串。