I am quite new to Swift, but I have been using User Defaults for a while now and never had any issue like this. I am using an alert with a textfield that request a clients name so I can append to my clientArray.
My issue is that the User Defaults reset after every app launch.
The code I have after the user saves the name is:
self.clientArray.append(clientName.text!)
self.defaults.setValue(clientArray, forKey: "Clients")
print(clientArray)
This works well and indeed every time I add a new name, I see on the console my array will all the client names on it with no issue, however, anytime I close my app, my array is completely empty and so is my User Defaults value, completely erasing the previous saved client names.
I have also tried to use it like this:
self.clientArray.append(clientName.text!)
self.defaults.set(clientArray, forKey: "Clients")
print(clientArray)
Just in case that it would behave differently, but it has not.
Could anyone point me what I am missing or point me the right direction? Other than this my app is running flawlessly.
Thank you in advance.
Update as requested:
The way I am reading the array is:
let clientArrayResult = self.defaults.array(forKey: "Clients")
print(\(clientArrayResult))