比较 NSDate 值和 EXC_BAD_ACCESS 错误时的奇怪问题
我正在开发一个程序,该程序比较日期值,如果新选择的日期小于 dateStart,则设置开始日期;如果新选择的日期大于 dateStart,则设置 dateEnd。
当我选择更大的值(同时设置 dateEnd)时,代码运行良好,但当我尝试设置 dateStart 时,程序崩溃了。这是我的代码:
- (void)setDate:(NSDate *)dateVal
{
NSComparisonResult result = [dateVal compare:dateStart];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy 00:00:00"];
switch (result)
{
case NSOrderedAscending:
{
dateStart = dateVal;
[dateFormatter setDateFormat:@"dd.MM.yyyy 00:00:00"];
[lblDateStart setText:[dateFormatter stringFromDate:dateStart]];
} break;
case NSOrderedDescending:{
dateEnd = dateVal;
[dateFormatter setDateFormat:@"dd.MM.yyyy 23:59:59"];
[lblDateEnd setText:[dateFormatter stringFromDate:dateEnd]];
} break;
case NSOrderedSame: NSLog(@"%@ is the same as %@", dateStart, dateVal); break;
default: NSLog(@"erorr dates %@, %@", dateStart, dateVal); break;
}
}
该代码给出了 EXC_BAD_ACCESS 错误
NSComparisonResult 结果 = [dateVal 比较:日期开始];
当我从日历视图中选择较小的日期值时。
我已经将 dateStart 和 dateEnd 设置为属性,并在开始时为它们设置了 [[NSDate alloc] init] 。请帮忙。
i'm working on a program that compares date values and sets the beggining date if the newly selected date is smaller than the dateStart and sets the dateEnd if the newly selected date is bigger than dateStart.
The code works well when i choose bigger values (while setting dateEnd) but program crashed when i try to set dateStart. heres my code:
- (void)setDate:(NSDate *)dateVal
{
NSComparisonResult result = [dateVal compare:dateStart];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy 00:00:00"];
switch (result)
{
case NSOrderedAscending:
{
dateStart = dateVal;
[dateFormatter setDateFormat:@"dd.MM.yyyy 00:00:00"];
[lblDateStart setText:[dateFormatter stringFromDate:dateStart]];
} break;
case NSOrderedDescending:{
dateEnd = dateVal;
[dateFormatter setDateFormat:@"dd.MM.yyyy 23:59:59"];
[lblDateEnd setText:[dateFormatter stringFromDate:dateEnd]];
} break;
case NSOrderedSame: NSLog(@"%@ is the same as %@", dateStart, dateVal); break;
default: NSLog(@"erorr dates %@, %@", dateStart, dateVal); break;
}
}
The code gives EXC_BAD_ACCESS error at
NSComparisonResult result = [dateVal
compare:dateStart];
when i select a smaller date value from the calendar view.
i've both set dateStart and dateEnd as property and [[NSDate alloc] init] for both of them at the beginning. please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,伙计们,我终于意识到了这个问题。
问题出在这一行:
但它必须是这样的:
谢谢,祝你有美好的一天。
ok guys i've finally realized the problem.
The problem was in this line:
but it has to be like this:
Thanks and have a nice day.