NSPredicate in an array of arrays

I have an array that, when printed, looks like this:

  (
         (
         databaseVersion,
         13
     ),
         (
         lockedSetId,
         100
     )
 )

Is it possible to filter this using NSPredicate (potentially by index in an array). So something like: give me all the lines where element 0 is "databaseVersion"? I know that if I had a lot of dictionaries, I could do it with a predicate similar to that found here , but I found that when using dictionaries and storing a lot of data, memory consumption increased (from ~ 80 mb to ~ 120 mb ), therefore, if possible, I would save the array. Any suggestions on how to do this?

+4
source share
2 answers

This can be done using "SELF [index]" in the predicate:

 NSArray *array = @[ @[@"databaseVersion", @13], @[@"lockedSetId", @100], @[@"databaseVersion", @55], @[@"foo", @"bar"] ]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF[0] == %@", @"databaseVersion"]; NSArray *filtered = [array filteredArrayUsingPredicate:pred]; NSLog(@"%@", filtered); 

Output:

 ( ( databaseVersion, 13 ), ( databaseVersion, 55 ) ) 

Or you can use a block-based predicate:

 NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(NSArray *elem, NSDictionary *bindings) { return [elem[0] isEqualTo:@"databaseVersion"]; }]; 
+7
source

You can simply use ANY in NSPredicate:

it works great

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF == %@", @"value"]; 

or

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF contains[cd] %@", @"value"]; 
+6
source

All Articles