0

I am having NSMutableDictionary (subjectValue) which contains value and keys. I want to filter based on the values searched for in search bar. So, I used the code like below.

NSArray *allvalues = [subjectValue allValues];
NSArray *filteredValues = [allvalues filteredArrayUsingPredicate:predicate];

Now, filteredValues will have the values that are based on the search word.

Now, I want to get the Keys also for the filtered values got from the above result. I know how to get all values and all, but i want to get the keys for the filtered result.

Please advise how can i get that?

THIS IS NOT DUPLICATE OF THIS QUESTION. Existing Query Link

What has been asked in the above link is, how to retrieve all values based on the predicate search. What I'm asking is beyond that. I already have got all filtered values, I want to get keys for that filtered values. My question is different.

Community
  • 1
  • 1
user1953977
  • 163
  • 2
  • 12
  • This is NOT duplicate of http://stackoverflow.com/questions/16556905/filtering-nsdictionary-with-predicate – user1953977 Jul 14 '15 at 07:33
  • What they are asking is, how to retrieve all values based on the predicate search. What I'm asking is beyond that. I already have got all filtered values, I want to get keys for that filtered values. This question is different from http://stackoverflow.com/questions/16556905/filtering-nsdictionary-with-predicate – user1953977 Jul 14 '15 at 07:34
  • Try http://stackoverflow.com/questions/7057063/get-all-keys-of-an-nsdictionary-as-an-nsarray – Chris Jul 14 '15 at 08:03
  • I know getting all Keys. Please check my question, I need to get Keys for some values only. – user1953977 Jul 14 '15 at 09:36
  • You could use `- keysOfEntriesPassingTest:` to get an `NSSet` of the keys (based on a predicate that would be evaluated against values) and then `- objectsForKeys:notFoundMarker:` to get an `NSArray` of the values. – Alladinian Jul 14 '15 at 09:46

2 Answers2

1

There is a function keysOfEntriesPassingTest in NSDictionary. Is that what you are looking for? Example:

NSDictionary *subjectValue = @{@"1":@"A", @"2":@"B", @"3":@"C", @"4":@"D"};
NSArray *filteredValues = @[@"B", @"D"];


NSSet *keys = [subjectValue keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
    return [filteredValues containsObject:obj];
}];
NSLog(@"%@", keys.description);

The output I get for that is {(4,2)} (NSSet)

Yaser
  • 408
  • 2
  • 6
  • No. I think, My question couldn't be understood. I have "VALUES" already. I.e, "NSMutableDictionary setObject: subjectValue Key:keys". In this, it contains all values "NSArray *allvalues = [subjectValue allValues];" Then, I am filtering to get some specific values based on the search word. This has put in "filteredValues" array. Now, I want to get the "KEYS" for which the "values (filteredValues)" have got. – user1953977 Jul 14 '15 at 10:26
  • In your sample, you have KEYS -> [@"B", @"D"] ? You are getting VALUE from this? Correct? – user1953977 Jul 14 '15 at 10:27
  • Also, [valuesToLookFor obj] line gives error as no visible interface for NSArray declares the selector obj – user1953977 Jul 14 '15 at 10:30
  • In my sample, the keys are the numbers i.e. 1, 2, 3 and 4 are the keys (sorry for this, the thing above is a quick way to create a dictionary but may not be so clear. The format is @{key1:value1, key2:value2...} I fixed my code above – Yaser Jul 14 '15 at 10:42
  • I modified the example to use what I think is your variable names – Yaser Jul 14 '15 at 10:51
0

I doubt that you can get the keys for given values by a predicate-like method. So if you have to enumerate the dictionary anyway, use something like this

 __block NSMutableDictionary * filteredDictionary = [NSMutableDictionary dictionary];
  [subjectValue enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if ([obj matchesTheConditionOfThePredicate]) {
      filteredDictionary[key] = obj;
    }
  }];
vadian
  • 274,689
  • 30
  • 353
  • 361