簡體   English   中英

如何在目標c中使用NSpredicate在2個值之間過濾數組? 我想通過名稱或版本過濾數組

[英]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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM