-2

I have been carrying values on my app through a global variable declared on the first View Controller. It's value is updated whenever the app is reopened.

When the user is on the same session, the array appends propperly, but when I save it through userDefaults, the elements position on the array get messed up. Can you find out why? I cannot seem to find an answer.

Array on same session: [0, 123456, 789101, 456789]

Array after loading from userDefaults: [(0, 123456, 789101, 456789), 222345] It wraps up everything that was done on parenthesis.

The same happens when I try to save a vector of integers. Is there any way I can save variables on userDefaults to get the following:

Vector before the save: [1, 2, 3]

Vector after the save: [1, 2, 3]

Sorry for not posting the full code, I am currently coding on a VM so the copying is hard. I thought explaining the issue would be better. That's how I've been saving and loading it:

var ACC = ["123456", "987654", "908761"]
NSUserDefaults.standardUserDefaults().setObject(ACC, forKey: "Key")
NSUserDefaults.standardUserDefaults().objectForKey("Key")
Gabriel Robaina
  • 709
  • 9
  • 24

1 Answers1

-2

For anyone having the same problem, all I had to do was force the object type to String, and equal it at index 0 in order for it to append correctly.

var ACC = ["123456", "987654", "908761"]
NSUserDefaults.standardUserDefaults().setObject(ACC, forKey: "Key")
NSUserDefaults.standardUserDefaults().objectForKey("Key")![0] as? [String]
Gabriel Robaina
  • 709
  • 9
  • 24