Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate for NSNumber property of NSManagedObject

I have NSNumber * year property of NSManagedObject, it's type in data model is Integer 16.

I try to check with NSPredicate for this year, but can't find the right one.

What I tried:

NSPredicate *p = nil;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
NSNumber *yearNo = [nf numberFromString:term];
if (yearNo) {
    p = [NSPredicate predicateWithFormat:@"(cars.year == %i)", yearNo.intValue];
}

I also tried:

NSPredicate *p = nil;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
NSNumber *yearNo = [nf numberFromString:term];
if (yearNo) {
    p = [NSPredicate predicateWithFormat:@"(cars.year == %@)", yearNo];
}

In both cases app crashes.

like image 426
Shmidt Avatar asked Jan 09 '13 17:01

Shmidt


1 Answers

If you provide more details for your model, we could help you.

But I think the problem is due to cars. If cars is to-many you need a modifier for this

[NSPredicate predicateWithFormat:@"ANY cars.year == %@", yearNo];
like image 139
Lorenzo B Avatar answered Sep 21 '22 12:09

Lorenzo B