【发布时间】:2011-11-02 13:55:25
【问题描述】:
在没有太多调试线索的情况下,我似乎无法找出导致我的应用程序严重崩溃的代码更改错误。
这是原来的方法
+ (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 小时之间时间戳的所有记录。我需要更改查询从现在到明天(次日午夜)之间时间戳的所有记录的方法,因此我根据this other stackoverflow question 将代码更新为此
+ (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];
包括应用程序崩溃。我通过将它们注释掉等方式将其缩小到这两行。
我完全不知道哪里出了问题。
【问题讨论】:
-
“应用程序崩溃”是什么意思?以什么方式?你得到什么堆栈跟踪?
-
在模拟器中运行时只是主循环硬崩溃
-
您需要获取回溯(在调试控制台中键入 bt),“崩溃”不会帮助任何人解决您的问题
-
哦,你确实需要那个被注释掉的版本,否则你会泄露原来的
components分配。
标签: objective-c nsdate nstimeinterval