本文介绍了构建nspredicate时EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算生日和今天之间的月数。
使用该数字,我正在构建一个谓词来从核心数据中获取对象。
虽然正确计算月数(如日志所示),但在构建谓词时我得到一个EXC_BAD_ACCESS。

I am calculating the number of months between a birthdate and today.With that number, I am building a predicate to fetch objects from core data.Although the number of months is calculated correctly (as the log shows), I am getting a EXC_BAD_ACCESS when building the predicate.

这是我的代码:

    NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

    NSUInteger unitFlags = NSMonthCalendarUnit;

    NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:birthdate
                                              toDate:today options:0];
    int months = [components month];
    NSLog(@"months: %ld", (long)months);
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(alter_min_monat > %@)", months];

为什么会发生这种情况?

Why is this happening?

推荐答案

问题是占位符。
%@ 不应与 int 一起使用,但使用 %d 代替。

The issue is the placeholder.%@ shouldn't be used with an int, but with a %d instead.

所以这一行:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(alter_min_monat > %@)", months];

应该是:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"(alter_min_monat > %d)", months];

其他相关信息:

这篇关于构建nspredicate时EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:00