user3033437 user3033437 - 4 months ago 5
iOS Question

NSMutableDictionary replicates the last inserted index in each inserted index instead of insert the new index

I've been googling for a while, trying to find a solution to my problem and ended here. I'm trying to create a dictionary of dictionaries to load all the data in tableViewCells.
So I need a key for "name", another key for "description", another key for "image name" and like this..

I'm doing an async call so I've created a global var,

which is a
and in the async
Function I've created another
named jsonValues.
I use jsonValues to store the data inside the loop. The data is stored with keys too :

jsonValues["name"] = Name

And at the end of the loop I store jsonValues inside nsDict, my NSMutableDictionary global variable.

nsDict.setObject(jsonValues, forKey: c)
c += 1

At this point there is already somebody that knows for sure my issue and my mistake. But I've been trying and reading a lot of stackoverflow and didn't find the way to do something that easy.

My dictionary, is getting filled by all the jsonValues, but instead of inserting diferent values, it's copying all of them. Which is, in the first round of the loop, it insert the first value (dictionary of values). In the second loop, it insert the second dictionary of values in the first and in the second index...
At the end I got 43 same dictionaries. All of them are the copy of the last one...

Does anybody know why? I've spent two hours with this issue. Some help would be very appreciated, thanks!

private func parseJson(json : NSMutableArray, tableView : UITableView){
var jsonValues = NSMutableDictionary()
nsDict = NSMutableDictionary.init(capacity: 10)
var c : Int = 0
for j in json {
var nsData : NSMutableDictionary
//clean array
// jsonValues.removeAll()

//Create main value
guard let value = j.valueForKey("value")?.valueForKey("value")! else{

//Get name
guard let Name : String = (value.valueForKey("Name")?.valueForKey("en") as? String) else {

jsonValues["name"] = Name

title = "Address: "

//Get Address
if let Address = value.valueForKey("Address")?.valueForKey("en") as? String{
jsonValues["Address"] = Address

title = "Country: "

//Get country
if let country = geoposition.valueForKey("country") as? String{
let fCountry = title+country
jsonValues["Country"] = fCountry

nsDict.setObject(jsonValues, forKey: c)
c += 1




just move your var jsonValues = NSMutableDictionary() inside cycle