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,
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>
)
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.
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.
%@ 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With