我有一个整数字段的表。我想选择该字段包含任何几个不同值的所有行。使用NSPredicate与整数列表匹配
我使用这个:
[NSPredicate predicateWithFormat:@"myIntValue IN {1,2,3,4}"]
但这只能返回行,其中myIntValue
为1
我在做什么错?
我有一个整数字段的表。我想选择该字段包含任何几个不同值的所有行。使用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]];
documentation可能在这里有用。
我不知道这种情况已经改变,因为这个问题最初发布,但我得到所有这三个完全相同的断言。
[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]];
所以不需要将所有东西都包裹在对象中。
核心数据只存储对象,因此当您创建谓词时,您总是必须将标量转换为对象。 – TechZen 2011-04-14 18:59:47