【问题标题】:NSDictionary Getting Repeated valuesNSDictionary 获取重复值
【发布时间】:2016-09-24 16:22:37
【问题描述】:

我知道这可能是一个重复的问题,但我搜索了很多但无法找到适合我的答案。

我有一个NSMutableArray,它有一个twoNSDictionary,其中包含我需要在UITableView 上填充的键和值。我已检索到 dictionary 的值,我将使用

填充该值
NSMutableArray *mutArray = [responseArray valueForKey:@"Table"];

我确实喜欢

NSMutableSet *names = [NSMutableSet set];
NSMutableArray *mutArray1 = [[NSMutableArray alloc] init];

for (id obj in mutArray) {

    NSString *destinationName = [obj valueForKey:@"AssetClassName"];

    if (![names containsObject:destinationName]) {

       [mutArray1 addObject:destinationName];
       [names addObject:destinationName];

    }
} 

因为值AssetClassName 重复了。现在我在mutArray1 中有三个值,我需要将它们显示为UITableView 部分。在AssetClassName 下,我有一些数据决定了该部分中的行。

为了检索我正在做的数据

for (int i = 0; i < [mutArray1 count]; i++) {

    NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *b = [[NSMutableDictionary alloc] init];

    for (NSDictionary *dict in mutArray) {

        if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) {

           [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"];
           [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"];
           [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"];
           [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"];

           [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]];

           [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]];
        }
    }
}  

mutdict 是一个全局声明的NSMutableDictionary,并在viewdidLoad 中实例化

mutdict = [[NSMutableDictionary alloc] init];

根据需要将值插入mutdict。每个SubAssetClassName 都会相应地添加到AssetclassName 中。

但我的问题是在我的最终字典中,即 mutdict 重复了 SubAssetClassName 的值。

谁能告诉我如何解决这个问题。

我的控制台

"AssetClassName" =     {

    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "HIGH YIELD BONDS";
        "ModelAllocationPercentage" = 22;
    };
};
"AssetClassName" =     {
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "EMERGING MARKETS EQUITIES";
        "ModelAllocationPercentage" = 10;
    };
};
"AssetClassName" =     {
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
    "SubAssetClass" =         {
        "%" = 0;
        "Amount (EUR)" = 0;
        "Investment Categories" = "STRUCTURED PRODUCTS";
        "ModelAllocationPercentage" = 10;
    };
};
}

在这里我可以看到每个部分的所有SubAssetClass 值都是相同的,但实际上并非如此。

我该如何解决这个问题。

【问题讨论】:

    标签: ios objective-c uitableview nsmutabledictionary


    【解决方案1】:

    您需要在循环内创建一个可变字典的新实例。现在,您创建一个实例并一遍又一遍地更新它。这会导致一个字典被一遍又一遍地添加。

    如下修改你的代码:

    for (NSInteger i = 0; i < [mutArray1 count]; i++) {
        NSMutableDictionary *b = [[NSMutableDictionary alloc] init];
    
        for (NSDictionary *dict in mutArray) {
            if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) {
               NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
    
               [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"];
               [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"];
               [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"];
               [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"];
    
               [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]];
    
               [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]];
            }
        }
    }  
    

    此外,在大多数情况下,您不应该使用valueForKey:。使用objectForKey:,除非您有明确且具体的需要使用键值编码,而不是简单地从字典中获取给定键的对象。

    【讨论】:

    • 谢谢你......我的 UITableView 单元格中还有一个问题,我得到了这些值,例如 NSDictionary *rowData = [mutdict valueForKeyPath:[sectionArray objectAtIndex:indexPath.section]];,但不知道如何使用 SubAssetClass 名称和值填充每个单元格。你能帮忙吗?
    • 如果您还有其他问题,请发布新问题。请务必在问题中包含所有相关详细信息。
    • 好吧,正如你所说,我问了另一个问题,你可以在link中查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多