如何在 objective c 中使用 NSPredicate 过滤两个值之间的数组?我想通过名称或版本过滤数组
How to filter an array between 2 values using NSpredicate in objective c ? I want to filter an array via name or version
(void)filterListForSearchText:(NSString *)searchText
{
NSPredicate *filterPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[cd]%@" ,searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.devices
filteredArrayUsingPredicate:filterPredicate]];
[self.tableView reloadData];
}
其中 self.device 日志是:::
数组是,
(
"<NSManagedObject: 0x7ffa32caab60> (entity: Device; id:
0xd000000000080000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p2> ; data: {\n company = c1;\n name = apple;\n version =
v1;\n})",
"<NSManagedObject: 0x7ffa32cb4100> (entity: Device; id: 0xd0000000000c0000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD/Device/p3> ; data: {\n company = \"com 2\";\n name = asad;\n version = v2;\n})",
"<NSManagedObject: 0x7ffa32cb4160> (entity: Device; id:
0xd000000000100000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p4> ; data: {\n company = \"company 3\";\n name = abbas;\n
version = v3;\n})",
"<NSManagedObject: 0x7ffa32ca8810> (entity: Device; id:
0xd000000000140000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p5> ; data: {\n company = ccc;\n name = pakistan;\n version
= vvv;\n})",
"<NSManagedObject: 0x7ffa32ca8870> (entity: Device; id:
0xd000000000180000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p6> ; data: {\n company = bbyiww;\n name = pine;\n version
= vvvrre;\n})"
)
像这样更改谓词
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd]%@ and SELF.version = %@" ,searchText, versionNo];
self.searchResults = [NSMutableArray arrayWithArray:[self.devices filteredArrayUsingPredicate:filterPredicate]];
[self.tableView reloadData];
你可以这样实现:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"(value1 != 0) AND (value2 == %@)", [NSNumber numberWithBool:YES]];
或
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"value1 contains[cd] %@ OR value2 contains[cd] %@", value1, value2];
(void)filterListForSearchText:(NSString *)searchText
{
NSPredicate *filterPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[cd]%@" ,searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.devices
filteredArrayUsingPredicate:filterPredicate]];
[self.tableView reloadData];
}
其中 self.device 日志是:::
数组是,
(
"<NSManagedObject: 0x7ffa32caab60> (entity: Device; id:
0xd000000000080000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p2> ; data: {\n company = c1;\n name = apple;\n version =
v1;\n})",
"<NSManagedObject: 0x7ffa32cb4100> (entity: Device; id: 0xd0000000000c0000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD/Device/p3> ; data: {\n company = \"com 2\";\n name = asad;\n version = v2;\n})",
"<NSManagedObject: 0x7ffa32cb4160> (entity: Device; id:
0xd000000000100000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p4> ; data: {\n company = \"company 3\";\n name = abbas;\n
version = v3;\n})",
"<NSManagedObject: 0x7ffa32ca8810> (entity: Device; id:
0xd000000000140000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p5> ; data: {\n company = ccc;\n name = pakistan;\n version
= vvv;\n})",
"<NSManagedObject: 0x7ffa32ca8870> (entity: Device; id:
0xd000000000180000 <x-coredata://7EE6EBA4-6F15-49D8-B994-2E02379A75CD
/Device/p6> ; data: {\n company = bbyiww;\n name = pine;\n version
= vvvrre;\n})"
)
像这样更改谓词
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd]%@ and SELF.version = %@" ,searchText, versionNo];
self.searchResults = [NSMutableArray arrayWithArray:[self.devices filteredArrayUsingPredicate:filterPredicate]];
[self.tableView reloadData];
你可以这样实现:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"(value1 != 0) AND (value2 == %@)", [NSNumber numberWithBool:YES]];
或
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"value1 contains[cd] %@ OR value2 contains[cd] %@", value1, value2];