Create NSDictionary from another NSDictionary?

I have an NSDictionary that registers this:

  address = "30 East 23rd Street"; address1 = "30 East 23rd Street"; address2 = ""; age = 32; alert = ""; "blood_pressure_1" = ""; "blood_pressure_2" = ""; bmi = ""; 

I want to create a new NSDictionary that contains only a few keys to the old one, for example, only age , bmi and alert . How can i do this?

+4
source share
4 answers
 NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [oldDict objectForKey:@"address"], @"address", [oldDict objectForKey:@"address1"], @"address1", [oldDict objectForKey:@"address2"], @"address2", nil]; 

You can obviously change the keys as needed ...

+3
source

It might be a little easier:

 NSArray *keys = [NSArray arrayWithObjects:@"address", @"address1", @"address2", nil]; NSDictionary *subset = [NSDictionary dictionaryWithObjects: [fullDictionary objectsForKeys:keys notFoundMarker:@""] forKeys:keys]; 
+9
source
 NSMutableDictionary* d = [NSMutableDictionary dictionaryWithDictionary: bigDict]; [d removeObjectsForKeys: [NSArray arrayWithObjects: @"address", @"age", nil]]; 
+2
source

Even easier

 NSDictionary *newDictionary = [oldDictionary dictionaryWithValuesForKeys:@[@"age", @"bmi", @"alert"]]; 
+1
source

Source: https://habr.com/ru/post/1382786/


All Articles