blub
blub

Reputation: 359

NSPredicate get Values of NSDictionary by selecting Keys with Beginswith

I have to manage this given ugly xml structure. How can i query all Key-Tags starts with / contains "Key" ?

Xml-Structure:

<Data>
    <Rec>
       <Key1>0001</Key1>
       <Key2>0015</Key2>
       <Key3>0002</Key3>
       <Key4>0022</Key4>
       ...              
    </Rec>
    <Rec>
       <Key1>0015</Key1>
       <Key2>0022</Key2>
       <Key3>0002</Key3>
       <Key4>0001</Key4>
       ...              
    </Rec>
    ...
</Data>

Rec-Object-Structure:

@interface Rec : NSObject
@property (nonatomic, copy) NSMutableDictionary *content;
@end

Here my Code that works to select on Key1 :

predicateRecs = [NSPredicate predicateWithFormat:@"content.%K == %@", @"Key1", @"0001"];

[result addObjectsFromArray:[recs filteredArrayUsingPredicate: predicateRecs]];

recs is a Type of NSArray and contains Rec - Objects. %K needs to be able to do something like: content.@allKeys BEGINSWITH %@ == %@ ,@"Key", @"0001"`

I want to get alls Keys with Value e.g. 0001 How can i do that ?

Thanks

Upvotes: 2

Views: 695

Answers (1)

Lefteris
Lefteris

Reputation: 14677

I would based on your comments use a Subquery then:

predicateRecs = [NSPredicate predicateWithFormat:@"SUBQUERY(FUNCTION(SELF, 'allKeys'), $k, SELF[$k] beginswith[cd] %@ AND SELF[$k] == %@).@count > 0", @"Key",@"0001"];

Dave DeLong is the predicate master: http://www.mactech.com/sites/default/files/Dave_DeLong-Power_Of_Predicates.pdf

and of course: https://stackoverflow.com/a/5570510/312312

Upvotes: 1

Related Questions