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?
NSMutableArrays andNSMutableDictionarys instead of Swift's nativeArrayandDictionarytypes?productDictionary, which I assume hasStrings for keys andStrings for values, you'd initialize it likevar productDictionary = [String : String](). To create an array of these, you usevar productArray = [[String : String]](). And on purely stylistic grounds, you may want to call the dictionary simplyproductand the arrayproducts.