Using NSPredicate to Match with an Integer

I have a table with an integer field. I want to select all rows in which this field contains any of several values.

I use this:

[NSPredicate predicateWithFormat:@"myIntValue IN {1,2,3,4}"] 

but this returns rows where myIntValue is 1.

What am I doing wrong?

+4
source share
3 answers

It works:

 NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSArray *idList = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idealForId IN $IDLIST"]; request.predicate = [predicate predicateWithSubstitutionVariables: [NSDictionary dictionaryWithObject:idList forKey:@"IDLIST"]]; 
+10
source

Have you tried this?

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myIntValue IN %@", [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]]; 

The documentation may be useful here.

+2
source

I do not know if this has changed since the question was originally published, but I get the same predicate from all three of them.

 [NSPredicate predicateWithFormat:@"myIntValue IN {1, 2, 3, 4}"]; [NSPredicate predicateWithFormat:@"myIntValue IN {%d, %d, %d, %d}", 1, 2, 3, 4]; [NSPredicate predicateWithFormat:@"myIntValue IN %@", @[@1, @2, @3, @4]]; 

Therefore, there is no need to wrap all objects.

0
source

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


All Articles