1

I am trying to add nsmutabledictionaries into my nsmutablearray but when the addobject is executed it overwrites the previous data with the new. If i try to add a string it works fine, but if i try to add a nsmutabledictionary it doesnt work fine. I know this question is stated before but it seems that i can not find it for the swift language. This is my code below:

    @IBAction func logAction(sender: UIButton) {

    let buttonRow = sender.tag
    let product_name = data2[buttonRow].name
    let product_price = data2[buttonRow].price
    let product_id = data2[buttonRow].id

    productDictionary["product_name"] = product_name
    productDictionary["price"] = product_price
    productDictionary["id"] = product_id

    let string = product_name
    productArray.addObject(productDictionary)

    print(productArray, "order ends here")

}

I have as globals the following variables :

var productArray = NSMutableArray()
var productDictionary = NSMutableDictionary()

What am i doing wrong here?

3
  • Is there a particular reason why you're working with NSMutableArrays and NSMutableDictionarys instead of Swift's native Array and Dictionary types? Commented Oct 18, 2015 at 14:42
  • The reason i am using NSMutableArray s and NSMutableDictionary s is that i need to make an array that consists dictionaries and arrays with dictionaries inside those dictionaries. For example in the specific viewcontroller that i am calling this function i need to construct an array that has dictionaries inside it and then add this array to another array. I tried to use the native Array and Dictionary but i was not able to handle it. I couldnt find also a good tutorial for nested arrays and dictionaries so... Commented Oct 18, 2015 at 14:49
  • You can certainly do this with the Swift native types, but unlike in Objective-C, you have to specify the type of data that they contain. To create a dictionary like your productDictionary, which I assume has Strings for keys and Strings for values, you'd initialize it like var productDictionary = [String : String](). To create an array of these, you use var productArray = [[String : String]](). And on purely stylistic grounds, you may want to call the dictionary simply product and the array products. Commented Oct 18, 2015 at 15:48

1 Answer 1

2

Every time your logAction() is called, you're changing the value of the global productDictionary. Since NSArray doesn't store copies of the values that you add to it, you're simply adding another reference to the global dictionary every time. If you're going to stick with using NSMutableArrays and NSMutableDictionary's (see my comment on your original post), then get rid of the global productDictionary and instead create a new one each time logAction() is called. In other words, replace

productDictionary["product_name"] = product_name
productDictionary["price"] = product_price
productDictionary["id"] = product_id
productArray.addObject(productDictionary)

with

var productDictionary = NSMutableDictionary() 
productDictionary["product_name"] = product_name
productDictionary["price"] = product_price
productDictionary["id"] = product_id
productArray.addObject(productDictionary)

And what does let string = product_name do?

Sign up to request clarification or add additional context in comments.

1 Comment

That solved my problem. Thank you. The string variable is there just for testing. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.