如何获取数组中包含NSPredicate的所有项

时间:2021-04-15 12:34:55

I have an array of objects and each has an id, and i want to get all items where item.objectID contains in an array of ids, how can i get that result ?

我有一个对象数组,每个对象都有一个id,我想要获取所有项目的位置。objective包含一个id数组,我如何得到这个结果?

What i tried to do but i have an error on creating predicateWithFormat: Unable to parse the format string:

我尝试做什么,但在创建predicateWithFormat时我有一个错误:无法解析格式字符串:

NSString *predicateFormat = [NSString stringWithFormat:@"SELF.itemID CONTAIN IN (1,2,3,4,5,6,7,8)"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFormat];
filteredData = [localData filteredArrayUsingPredicate:predicate];

I just what to avoid this:

我只是想避免:

NSString *predicateFormat = [NSString stringWithFormat:@"SELF.itemID = 1 OR SELF.itemID = 2 OR SELF.itemID = 3"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: predicateFormat];
filteredData = [localData filteredArrayUsingPredicate:predicate];

because there is other condition to add for filter.

因为还有其他条件可以添加过滤器。

2 个解决方案

#1


5  

You almost had it :)

你几乎得到它:)

    NSArray *objects = @[
        @{
            @"itemID" : @1
        },
        @{
            @"itemID" : @2
        },
        @{
            @"itemID" : @3
        },
        @{
            @"itemID" : @4
        },
        @{
            @"itemID" : @5
        }
    ];

    NSArray *idsToLookFor = @[@3, @4];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN %@", idsToLookFor];
    NSArray *result = [objects filteredArrayUsingPredicate:predicate];

    NSLog(@"result: %@", result);

And if you do not want to pass in any array, but write the predicate "in hand", the syntax would be:

如果您不想传入任何数组,而是写入谓词“in hand”,则语法为:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN { 3, 4 }"];

And the result will be:

结果是:

result: (
        {
        itemID = 3;
    },
        {
        itemID = 4;
    }
)

#2


5  

Only IN needed:

只有在需要:

NSArray * desiredIDs = @[@1, @2, @3, @4, @5];
NSString * predicateFormat = [NSString stringWithFormat:@"SELF.itemID IN %@", desiredIDs];
...

#1


5  

You almost had it :)

你几乎得到它:)

    NSArray *objects = @[
        @{
            @"itemID" : @1
        },
        @{
            @"itemID" : @2
        },
        @{
            @"itemID" : @3
        },
        @{
            @"itemID" : @4
        },
        @{
            @"itemID" : @5
        }
    ];

    NSArray *idsToLookFor = @[@3, @4];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN %@", idsToLookFor];
    NSArray *result = [objects filteredArrayUsingPredicate:predicate];

    NSLog(@"result: %@", result);

And if you do not want to pass in any array, but write the predicate "in hand", the syntax would be:

如果您不想传入任何数组,而是写入谓词“in hand”,则语法为:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN { 3, 4 }"];

And the result will be:

结果是:

result: (
        {
        itemID = 3;
    },
        {
        itemID = 4;
    }
)

#2


5  

Only IN needed:

只有在需要:

NSArray * desiredIDs = @[@1, @2, @3, @4, @5];
NSString * predicateFormat = [NSString stringWithFormat:@"SELF.itemID IN %@", desiredIDs];
...