I'm running into an issue while attempting to move a user defaults array into a second array so I may be able to load it into a table view.
my userdefault is just a single field of type Int. I'm able to store data in the UD and when I display it it shows optional values:
[176, 177]
I have the following declared at the top level of my view:
let defaults = UserDefaults.standard
var intArray = [[Int:Any]]()
In viewDidLoad I have the following:
override func viewDidLoad() {
super.viewDidLoad()
intArray = defaults.array(forKey: "Favorites") as? [[Int: Any]] ?? []
print(defaults.array(forKey: "Favorites"))
}
The intArray is always empty but when I print the Favorites UserDefault it shows as it does above.
Obviously, I'm doing something wrong here; any help would be greatly appreciated.
Right now, you're trying to decode an array of Dictionaries with Int
as a key and Any
as a value (the outer set of brackets is the array, the inner brackets with the :
is the Dictionary
).
If you replace all occurrences of [[Int:Array]]
with [Int]
, your code will work.