FelipeDev.-
FelipeDev.-

Reputation: 3143

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!

Upvotes: 3

Views: 3514

Answers (2)

Khalil
Khalil

Reputation: 238

Try this,

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

NSArray * videoArray = [storeListArray filteredArrayUsingPredicate:predicate];

Upvotes: 2

Natarajan
Natarajan

Reputation: 3271

have you tried as like below,

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name == %@", filterString];

NSArray * videoArray = [storeListArray filteredArrayUsingPredicate:predicate];

Thanks!

Upvotes: 0

Related Questions