EXC_BAD_ACCESS when creating nspredicate

I calculate the number of months between the date of birth and today. With this number, I create a predicate to extract objects from the master data. Although the number of months is calculated correctly (as the log shows), I get EXC_BAD_ACCESS when building the predicate.

Here is my code:

    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?

+4
source share
1 answer

The question is the placeholder. %@should not be used with int, but with %d.

So this line:

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

Must be:

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

Other Related Information: String Programming Guide: String Format Specifiers

+21
source

Source: https://habr.com/ru/post/1540229/


All Articles