比较 NSDate 值和 EXC_BAD_ACCESS 错误时的奇怪问题

发布于 2024-10-19 14:29:20 字数 1288 浏览 4 评论 0原文

我正在开发一个程序,该程序比较日期值,如果新选择的日期小于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2024-10-26 14:29:20

好吧,伙计们,我终于意识到了这个问题。

问题出在这一行:

dateStart = dateVal;

但它必须是这样的:

[self setdateStart:dateVal];

谢谢,祝你有美好的一天。

ok guys i've finally realized the problem.

The problem was in this line:

dateStart = dateVal;

but it has to be like this:

[self setdateStart:dateVal];

Thanks and have a nice day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文