将一个字典的值与另一个字典的键进行比较
Compare values of one dictionary to keys of another dictionary
我有两本字典,如下所示:
Dictionary 1:
keyIdDict {
"Feb 5, 2015" = (
607
);
"Nov 9, 2009" = (
431,
433,
435
);
}
Dictionary 2:
rollImageDict {
607 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg";
433 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009124.jpg";
417 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009125.jpg";
419 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009126.jpg";
421 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009129.jpg";
435 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009130.jpg";
}
我想用 "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg"
替换第一个字典中的值,即 607
预期最终结果:
keyIdDict {
"Feb 5, 2015" = (
"/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg"
);
}
有人可以帮我解决这个问题吗?
我没有mac写代码但是很简单,按照步骤来就行了
先遍历字典(Dictionary 1),得到变量中的值
你会得到类似
的东西
607 //first iteration
431,433,435 // second iteration
然后再次拆分该值并再次迭代它。
所以现在,当您拆分和迭代时,您将获得 607
作为值,对每个值进行迭代。
每次迭代都从其他字典中获取值
完成..
我没有测试,但你可以试试这个代码
NSMutableDictionary *newDic = [[NSMutableDictionary alloc] initWithCapacity:0];
for (NSNumber *rollNum in rollImageDict) {
for (NSString *str in keyIdDict) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:0];
for (NSNumber *keyNum in keyIdDict[str]) {
if (rollNum == keyNum) {
[temp addObject:rollImageDict[rollNum]];
}
}
newDic[str] = temp;
}
}
给你:
NSMutableDictionary *newDictionary = [NSMutableDictionary dictionary];
[keyIdDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
NSArray *array = [rollImageDict objectsForKeys:obj notFoundMarker:[NSNull null]];
[newDictionary setObject:array forKey:key];
}];
我有两本字典,如下所示:
Dictionary 1:
keyIdDict {
"Feb 5, 2015" = (
607
);
"Nov 9, 2009" = (
431,
433,
435
);
}
Dictionary 2:
rollImageDict {
607 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg";
433 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009124.jpg";
417 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009125.jpg";
419 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009126.jpg";
421 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009129.jpg";
435 = "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009130.jpg";
}
我想用 "/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg"
607
预期最终结果:
keyIdDict {
"Feb 5, 2015" = (
"/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg"
);
}
有人可以帮我解决这个问题吗?
我没有mac写代码但是很简单,按照步骤来就行了
先遍历字典(Dictionary 1),得到变量中的值
你会得到类似
的东西607 //first iteration
431,433,435 // second iteration
然后再次拆分该值并再次迭代它。
所以现在,当您拆分和迭代时,您将获得 607
作为值,对每个值进行迭代。
每次迭代都从其他字典中获取值
完成..
我没有测试,但你可以试试这个代码
NSMutableDictionary *newDic = [[NSMutableDictionary alloc] initWithCapacity:0];
for (NSNumber *rollNum in rollImageDict) {
for (NSString *str in keyIdDict) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:0];
for (NSNumber *keyNum in keyIdDict[str]) {
if (rollNum == keyNum) {
[temp addObject:rollImageDict[rollNum]];
}
}
newDic[str] = temp;
}
}
给你:
NSMutableDictionary *newDictionary = [NSMutableDictionary dictionary];
[keyIdDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
NSArray *array = [rollImageDict objectsForKeys:obj notFoundMarker:[NSNull null]];
[newDictionary setObject:array forKey:key];
}];