开发者

Using NSPredicate to match against a list of integers

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜