Reputation: 1334
I have an array and it has lots of dictionary's keys it comes from API. My array as follows
Dictionary keys array :
NSArray *arr = @[@"01", @"02", @"03"];
Dictionary with key-value pairs
NSDictionary *dic = @{@"01": @"Hero", @"02" : @"Enemy", @"03" : @"Boss"};
Basically i want to match array values corresponding to dictonary keys without using array. I found a solition about that but I don't want to use for-loop for every cell(I have a lots of cell). My solution is like that
for(NSString *item in arr) {
[convertedArr addObject:[dic valueForKey:item]];
}
NSLog(@"%@", [convertedArr componentsJoinedByString:@","]);
Asumme have an array like this (1,2,3) and dictionary looks like {1 = "a", 2 = "b", 3 = "c"} I just want to give an array and it should return dictionary values like this ("a","b","c")
Anybody should give me better approach without using array? Thanks.
Upvotes: 0
Views: 358
Reputation: 539685
You can replace your for-loop by
NSArray *convertedArr = [dic objectsForKeys:arr notFoundMarker:@""];
which is at least less code. (The notFoundMarker:
is added for all keys
in the array which are not present in the dictionary. Your code would crash
in that situation.)
It might perform slightly better because it is a library
function. But I doubt that the difference is big, because in any case a dictionary
loopup is required for all keys in arr
.
Upvotes: 1