简体   繁体   中英

Filter an array with predicate in iOS

I have an array of JSONModel objects. The JSONModel object looks like this

@interface ProductModel : JSONModel

@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *price;
@property(nonatomic, strong) NSArray *productCategories;
@end

Inside the productCategory model I have this

@interface ProductCategoryModel : JSONModel

@property (nonatomic,assign) NSString *id;

@end

The json looks like this

{
    "productCount":"129",

    "products": [
        {
            "name": "Art attack",
            "price": "$19.99",
            "prouctCategories":[

                { "id": "50" }

                { "id": "10" }

             ]
}

I have to filter the array checking the id property of a productCategory in the array productCategories.One product can have multiple categories. I need to get for example the list of products with the id : 10. I want to get that list using filteredArrayUsingPredicate method of an NSArray but I cannot find the right predicate format to get this. Is it possible to do that using predicate. I tried this predicate but I didn't get anything NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ == '%@"', @"id", @"50"];

NSPredicate documentation is pretty clear about building this kind of queries. You can use ANY clause to apply filter on many-to-many relationships to get the products whose any of the category's ID is 10.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY productCategories.id == '10')"];

NSArray *filteredProducts = [products filteredArrayUsingPredicate:predicate];

To simplify the problem change the json to look like this:

{
"productCount":"129",

"products": [
    {
        "name": "Art attack",
        "price": "$19.99",
        "productCategories":["50","10"]
}

So you can use such predicate:

[NSPredicate predicateWithFormat:@"productCategories CONTAINS '10')"]

You can use NSPredicate as your json is like

{
"productCount":"129",

"products": [
    {
        "name": "Art attack",
        "price": "$19.99",
        "prouctCategories":[

            { "id": "50" }

            { "id": "10" }

         ]

}

then you may check it with this:

NSArray *arrFiltered = [arrProducts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(self.prouctCategories.id == '10')"]];

Or you may also change your json format to make things more easier like

{
"productCount":"129",

"products": [
    {
        "name": "Art attack",
        "price": "$19.99",
        "prouctCategories":[

            { "id": 50,10,20 }

         ]

}

So you may filtered id like this

NSArray *arrFiltered = [arrProducts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(self.prouctCategories.%K CONTAINS '10')", 'id']];

Hope it will help you. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM