Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate to filter Custom objects based on its property from Nested structure

I am trying to use NSPredicate to filter all CustomObjects from structure shown below, and having value true for their property "isSelected". I have an Nested structure like : isSelectedProperty-Object-NSArray-NSDictionary-NSArray.

[
  {
    "title": "ABC",
    "list": [
      <CustomObject>.isSelected = true,
      <CustomObject>.isSelected = true,
      <CustomObject>.isSelected = true
    ]
  },
  {
    "title": "ABC",
    "list": [
      <CustomObject>.isSelected = false,
      <CustomObject>.isSelected = true,
      <CustomObject>.isSelected = true
    ]
  },
  {
    "title": "ABC",
    "list": [
      <CustomObject>.isSelected = false,
      <CustomObject>.isSelected = true,
      <CustomObject>.isSelected = true
    ]
  }
]

From such Nested structure I need to filter all CustomObject having isSelected = true . So My Questions are,

  • Is it possible using NSPredicate?
  • Is Yes, then what will the predicate statement to filter this structure ?

Please provide some understanding so that we can understand how to actually deal with such structures.

EDIT - Very Near to Solution

After googling and help of answer of Muhammad Waqas, I succeed in filtering Array as below using

NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"list.isSelected CONTAINS[c] %@",@true];
NSArray *aArray = [mutArrContacts filteredArrayUsingPredicate:aPredicate];
NSArray *UnWrapped = [aArray valueForKey:@"list"];



<__NSArrayI 0x7fc969cde360>(
<__NSArrayM 0x7fc969f54a10>(
<ContactData: 0x7fc969f7a590>,
<ContactData: 0x7fc969f8dee0>
)
,
<__NSArrayM 0x7fc969f736f0>(
<ContactData: 0x7fc969f68310>
)
,
<__NSArrayM 0x7fc969f737a0>(
<ContactData: 0x7fc969f70340>
)
,
<__NSArrayM 0x7fc969f87430>(
<ContactData: 0x7fc969f65170>
)
,
<__NSArrayM 0x7fc969f874d0>(
<ContactData: 0x7fc969f51690>
)

)

But Now I am struggling to filter this Objects into Single array like

(
<ContactData: 0x7fc969f7a590>,
<ContactData: 0x7fc969f8dee0>,
<ContactData: 0x7fc969f68310>,
<ContactData: 0x7fc969f70340>,
<ContactData: 0x7fc969f65170>,
<ContactData: 0x7fc969f51690>
)
like image 303
Mrug Avatar asked Mar 25 '15 07:03

Mrug


People also ask

How to use predicates in NSArray and nsmutable?

NSDictionary can use predicates by filtering its keys or values (both NSArray objects). NSOrdered Set can either create new ordered sets from a filtered NSArray or NSSet, or alternatively, NSMutable Set can simply remove Objects In Array:, passing objects filtered with the negated predicate.

What is a predicate in nsfetchrequest?

NSFetchRequest has a predicate property, which specifies the logical conditions under which managed objects should be retrieved. The same rules apply, except that predicates are evaluated by the persistent store coordinator within a managed object context, rather than collections being filtered in-memory.

What does %%@ mean in nspredicate?

%@ is a var arg substitution for an object value—often a string, number, or date. %K is a var arg substitution for a key path. $VARIABLE_NAME is a value that can be substituted with NSPredicate -predicateWithSubstitutionVariables:. =, ==: The left-hand expression is equal to the right-hand expression.

How to use variable value in nspredicate?

As it is relatively time consuming for the app to parse the format string of the NSPredicate, we should try to reduce creating multiple NSPredicate and reuse similar NSPredicate as much as possible. We can use variable value in NSPredicate denoted by $ sign, and substitute its value by calling withSubstitutionVariables method.


1 Answers

YES you can filter custom objects using NSPredicate like this

NSPredicate *predicate = [NSPredicate    predicateWithFormat:@"ANY list.isSelected = %@",@true];
NSArray *filteredArry=[[json filteredArrayUsingPredicate:predicate] copy];

hope this will help you.

like image 189
Muhammad Waqas Bhati Avatar answered Oct 13 '22 00:10

Muhammad Waqas Bhati