使用NSPredicate匹配整数列表

问题描述

| 我有一个带有整数字段的表。我想选择该字段包含一些不同值的所有行。 我正在使用这个:
[nspredicate predicateWithFormat:@\"myIntValue IN {1,2,3,4}\"]
但这只会返回
myIntValue
为1的行。 我究竟做错了什么?     

解决方法

这有效:
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\"]];
    ,你有尝试过吗?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@\"myIntValue IN %@\",[NSArray arrayWithObjects:@\"1\",@\"2\",@\"3\",@\"4\",nil]];
该文档可能在这里有用。     ,自问题最初发布以来,我不知道这种情况是否已更改,但是我从这三个问题中得到的谓词完全相同。
[NSPredicate predicateWithFormat:@\"myIntValue IN {1,2,3,4}\"];

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

[NSPredicate predicateWithFormat:@\"myIntValue IN %@\",@[@1,@2,@3,@4]];
因此,无需将所有内容包装在对象中。