使用另一个数组过滤数组(CS 和谓词)
以下代码打印以下行:
TestApp[1156:207] Array: (
"Type A"
)
但不正确。 NSPredicate
应忽略区分大小写。
有谁知道我做错了什么?
NSArray *array = [[NSArray alloc] initWithObjects:[[TestObject alloc] initWithType:@"Type A"],
[[TestObject alloc] initWithType:@"Type B"],
[[TestObject alloc] initWithType:@"Type C"],
[[TestObject alloc] initWithType:@"Type D"],
[[TestObject alloc] initWithType:@"Type E"], nil];
NSArray *filter = [[NSArray alloc] initWithObjects:@"Type A", @"Type d", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF.type IN[cd] %@", filter];
NSLog(@"Array: %@", [array filteredArrayUsingPredicate:predicate]);
The following code print the line below:
TestApp[1156:207] Array: (
"Type A"
)
But isn't right. The NSPredicate
should ignore the case sensitive.
Does anyone know what I'm doing wrong?
NSArray *array = [[NSArray alloc] initWithObjects:[[TestObject alloc] initWithType:@"Type A"],
[[TestObject alloc] initWithType:@"Type B"],
[[TestObject alloc] initWithType:@"Type C"],
[[TestObject alloc] initWithType:@"Type D"],
[[TestObject alloc] initWithType:@"Type E"], nil];
NSArray *filter = [[NSArray alloc] initWithObjects:@"Type A", @"Type d", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF.type IN[cd] %@", filter];
NSLog(@"Array: %@", [array filteredArrayUsingPredicate:predicate]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此线程中提出了类似的问题: Case insensitive NSPredicate for strings from an数组?
看来 IN 不支持 [c] 修饰符。
您的情况的解决方案是更改查询,例如使用 CONTAINS。
[NSPredicate predicateWithFormat: @"ANY %@ CONTAINS[cd] SELF.type", filter]
The similar issue was raised in this thread: Case insensitive NSPredicate for strings from an array?
It seems that IN does not support [c] modifier.
Solution for your case is to change the query, e.g. to use CONTAINS.
[NSPredicate predicateWithFormat: @"ANY %@ CONTAINS[cd] SELF.type", filter]