I have a weird problem - my mutable array stores only 2 objects - its count is 1,2,2,2 and doesn't change. My only observation is that it always replaces old values f.x. I have: value1 - 2, value2 - 4 and I add next object f.x. 66 and my array look like this: value1 - 4, value2 - 66. Here is some code:
func appendArrays(product: NSString, bPrice: NSString, sPrice: NSString) {
defaults.synchronize()
if prodArr.count == 0 {
prodArr = NSMutableArray(array: [defaults.valueForKey(keys.keyProduct)!])
bpArr = NSMutableArray(array: [defaults.valueForKey(keys.keyBPrice)!])
spArr = NSMutableArray(array: [defaults.valueForKey(keys.keySPrice)!])
}
prodArr.addObject(product)
bpArr.addObject(bPrice)
spArr.addObject(sPrice)
defaults.setObject(prodArr, forKey: keys.keyProduct)
defaults.setObject(bpArr, forKey: keys.keyBPrice)
defaults.setObject(spArr, forKey: keys.keySPrice)
defaults.synchronize()
NSLog("%i",[defaults.valueForKey(keys.keyBPrice)!].count)
}
Of course you can have not more than two objects because your
spArr
is declared locally and it means that you create a newspArr
object each time you callappendArrays
. You need to create itvar spArr: NSMutableArray = NSMutableArray()
outside the function and instead of doing:do