将一个字典的值与另一个字典的键进行比较

时间:2016-06-22 11:37:44

标签: objective-c macos cocoa nsdictionary nsmutabledictionary

我有两个字典,如下面的一个:

 define(['Scripts/knockout-3.4.0', 'text!./ColumnSorting.html'], function (ko, htmlString) {
            function ColumnSortingViewModel(params) {
                // Set up properties, etc.
                if (typeof params.sProductCode === 'function') this.sProductCode = params.sProductCode();
                else if (typeof params.sProductCode === 'string') this.sProductCode = params.sProductCode;

                this.bIsReady = params.bIsReady();
                this.aColumns = params.aColumns();
                this.bShowingColumnSortMenu = ko.observable(false);
            }

            // PUBLIC FUNCTIONS
            ColumnSortingViewModel.prototype.hideProductCode = function _hideProductCode() {
                this.bShowingColumnSortMenu(false);
            };
            ColumnSortingViewModel.prototype.showProductCode = function _showProductCode() {
                this.bShowingColumnSortMenu(true);
            };

            // Return component definition
            return { viewModel: ColumnSortingViewModel, template: htmlString };
        });

    <div class="splitColumns" data-bind="css: sProductCode">
    <a class="button" data-bind="click: showProductCode ">Sort Columns</a>

<div data-bind="text: bShowingColumnSortMenu"></div> <!-- this show the observable function itself, "function c(..." -->
<div data-bind="text: bShowingColumnSortMenu()"></div> <!-- this shows the intended value, "false" -->
<div data-bind="text: bIsReady"></div>
</div>

<div class="splitColumns" data-bind="css: sProductCode">
    <a class="button" data-bind="click: showProductCode ">Sort Columns</a>
</div>

<!--ko if: bShowingColumnSortMenu()-->
<div class="regular" data-bind="css: sProductCode">
    <a class="button" data-bind="click: hideProductCode ">Close</a>
</div>
<!--/ko-->

我想用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"; }

替换第一个字典中的值,607

预期的最终结果:

"/Users/betteruse/Pictures/iPhoto Library.photolibrary/Masters/2014/10/21/20141021-160023/06122009123.jpg"

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:0)

我没有让我的mac编写代码,但它很简单,只需按照步骤

首先迭代字典(字典1)并获取变量值

你会有像

这样的东西
607 //first iteration
431,433,435 // second iteration

然后再次拆分该值并再次迭代它。

所以现在当你拆分和迭代时,你将607作为值,迭代每个值。

每次迭代

从其他字典中获取值

完成..

答案 1 :(得分:0)

我没有测试,但您可以尝试此代码

    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;
        }
    }

答案 2 :(得分:0)

你走了:

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];
}];