Jestin Saji Chacko Jestin Saji Chacko - 2 months ago 7
iOS Question

NSDictionary Getting Repeated values

I know this may be a repeated question but I googled a lot but not able to find a suitable answer for me.

I have a

NSMutableArray
which has
two
NSDictionary
with Keys and values which I need to populated on a
UITableView
. I have retrieved the value of the
dictionary
which I'm going populate using

NSMutableArray *mutArray = [responseArray valueForKey:@"Table"];


And I did like

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

}
}


Because the value
AssetClassName
is repeated. Now I have three values in
mutArray1
which I need to show as
UITableView
section. Under
AssetClassName
I have Some data which determines the row in that section.

For retrieving that data I'm doing like

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
is a
NSMutableDictionary
declared globally and is instantiate in
viewdidLoad


mutdict = [[NSMutableDictionary alloc] init];


The values are inserted into
mutdict
as I needed. Each
SubAssetClassName
is added into
AssetclassName
accordingly.

But my problem is in my final dictionary i.e
mutdict
the values for
SubAssetClassName
is repeated.

Can anybody tell how to solve this.

My console

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


Here I can see that all
SubAssetClass
values are same for each section but actually its not.

How can I solve this.

Answer

You need to create a new instance of your mutable dictionary inside the loop. Right now you create one instance and update it over and over. This results in one dictionary being added over and over.

Change you code as follows:

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

Also, in most cases you should not be using valueForKey:. Use objectForKey: unless you have a clear and specific need to use key-value coding instead of simply getting an object from the dictionary for a given key.