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)
a. Writeato a dictionary. Create an arraybby readingafrom a dictionary. Append something toa. Printb.