3

I am trying to make a mechanism that lets users save items to favourites (which is an array) and then save that to UserDefaults, however I can't seem to get it to write to UD correctly. Printing UDSaved in the example below returns [], even though I just appended an element to the array.

Does anyone have an idea what I'm doing wrong?

Is it a syntax error and I am not retrieving this correctly or is there a problem with my logic?

var favouritesArray: [String] = []

func UDWrite() {UserDefaults.standard.set(favouritesArray, forKey: "UDfavouritesArray")}
let UDSaved = UserDefaults.standard.stringArray(forKey: "UDfavouritesArray") ?? [String]()


favouritesArray.append("element")
UDWrite()

print(UDSaved)
4
  • 1
    You don’t appear to be re-reading UDSaved after you have updated the defaults. Commented Jun 24, 2018 at 12:45
  • 1
    you are saving empty array and then getting it, after that adding string, try to add favouritesArray.append("element"), right after var favouritesArray: [String] = [] Commented Jun 24, 2018 at 12:45
  • 1
    You do the following: Create an empty array a. Write a to a dictionary. Create an array b by reading a from a dictionary. Append something to a. Print b. Commented Jun 24, 2018 at 12:46
  • @DavidStockinger the function to write a to the dictionary is not called after a is created, it's called after I append something to a. So the order would be: - create an empty array A - create an array B by reading A from the dictionary - append something to A - write A to the dictionary B I realise though I need to assign B the value of A as part of my function, and this seems to work. Commented Jun 24, 2018 at 12:53

1 Answer 1

3

As some stated in the comments: UDSaved never get's updated. My solution would be to make UDSaved a function like so:

var favouritesArray: [String] = []

func UDWrite() {UserDefaults.standard.set(favouritesArray, forKey: "UDfavouritesArray")}
func UDSaved() -> [String] {return UserDefaults.standard.stringArray(forKey: "UDfavouritesArray") ?? [String]}()

favouritesArray.append("element")
UDWrite()

print(UDSaved())

Or you could make it a computed property

var UDSaved: [String] {
   return UserDefaults.standard.stringArray(forKey: "UDfavouritesArray") ?? [String]
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, and everyone who commented. I understand my mistake now and I fixed it by adding UDSaved = favouritesArray to my UDWrite() function and this fixed the problem.
Well UDSaved = favouritesArray helps as long as your app is running but this won't help to restore state after relaunching your app!
@CristianMoisei I'd highly recommend not doing that. When you can avoid modifying external variables inside functions, you should do so to keep your functions pure. Making UDSaved a computed property is a much better solution.
You're right @HerrderTöne I'll take some time to wrap my head around your solution (I'm relatively new to this) and do it that way. Thanks again.

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.