I have a table with an integer field. I want to select all rows where that field contains any of a few different values.
I'm using this:
[NSPredicate predicateWithFormat:@"myIntValue IN {1,2,3,4}"]
but this only returns rows where myIntValue
is 1.
What am I doing wrong?
This 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"]];
Have you tried this?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myIntValue IN %@",
[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]];
The documentation may be of use here.
I don't know if this has changed since the question was originally posted but I get the exact same predicate from all three of these.
[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]];
So no need to wrap everything in objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With