Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Arrays in Objects using NSPredicate

I need to filter an array by a property from an object that is inside another array. Let's say that I got two classes like:

StoreList.h

@interface StoreList : NSObject {
  NSMutableArray *storesArray; //Array containing Store objects
}

Store.h

@interface Store : NSObject {
  NSString *name;
}

So, I have an NSArray (storeListArray) that have some StoreList objects in it. Then, my Array is something like this:

storeListArray = [
 StoreList:{
    storesArray: {
                  stores[{
                    store: {
                     name: "Store1"
                    },
                    store: {
                     name: "Store2"
                    }
                  }]
                },
    storesArray: {
                  stores[{
                    store: {
                     name: "Store1"
                    },
                    store: {
                     name: "Store2"
                    }
                  }]
                }
   }
];

Well, my question is: How can I filter storeListArray by the "name" property of the Store Object, using NSPredicate?

I was trying to do something like this, but this don't work:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY storeList CONTAINS[cd] %@", filterString];
self.filteredStores = [storeListArray filteredArrayUsingPredicate:predicate];
return self.filteredStores;

Thanks for helping!

like image 816
FelipeDev.- Avatar asked Aug 18 '26 21:08

FelipeDev.-


1 Answers

Try this,

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"ANY SELF.storesArray.name == %@", filterString];

NSArray * videoArray = [storeListArray filteredArrayUsingPredicate:predicate];
like image 116
Khalil Avatar answered Aug 20 '26 10:08

Khalil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!