在没有太多调试线索的情况下,我似乎无法找出导致我的应用程序硬崩溃的代码更改错误。
这是原始方法
+ (NSArray *)currentReservations {
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
double futureTimeframe = interval + SecondsIn24Hours;
NSArray *reservations = [Reservation findWithSql:@"select * from Reservation where timestamp < ? and timestamp > ?" withParameters:[NSArray arrayWithObjects:[NSNumber numberWithDouble:ceil(futureTimeframe)], [NSNumber numberWithDouble:floor(interval)], nil]];
return reservations;
}
该方法设置了一些变量,因此我可以查询数据库以查找具有现在到未来 24 小时之间时间戳的所有记录。我需要更改方法以查询从现在到明天(第二天午夜)之间带有时间戳的所有记录,因此我根据其他 stackoverflow 问题将代码更新为此
+ (NSArray *)currentReservations {
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1]; // tomorrow
NSDate *tomorrow = [gregorian dateByAddingComponents:components toDate:today options:0];
// [components release]; // dont think we need this release, but it is in the example here: https://stackoverflow.com/questions/181459/is-there-a-better-way-to-find-midnight-tomorrow
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
components = [gregorian components:unitFlags fromDate:tomorrow];
[components setHour:0];
[components setMinute:0];
NSDate *tomorrowMidnight = [gregorian dateFromComponents:components];
[components release], components=nil;
[gregorian release], gregorian=nil;
NSTimeInterval interval = [today timeIntervalSince1970];
NSTimeInterval tomorrowInterval = [tomorrowMidnight timeIntervalSince1970];
NSArray *reservations = [Reservation findWithSql:@"select * from Reservation where timestamp < ? and timestamp > ?" withParameters:[NSArray arrayWithObjects:[NSNumber numberWithDouble:tomorrowInterval], [NSNumber numberWithDouble:floor(interval)], nil]];
return reservations;
}
但是,当这两行:
NSTimeInterval interval = [today timeIntervalSince1970];
NSTimeInterval tomorrowInterval = [tomorrowMidnight timeIntervalSince1970];
包括应用程序崩溃。我通过将它们注释掉等将其缩小到这两行。
我完全不知道出了什么问题。