Goal: is to save array data to be retrieved by next session
Approach: encode the array to Json, store in UserDefaults, then retrieve data and decode to the array. Maybe a better way, assume wrapping in Json is the best way to transport to UserDefaults
I think my encode and save to UserDefault works & I can return the object and print a size... don't know how to decode back to my array?
var matrixArray = [matrixItem]()
var returnArray: Decodable = [returnMatrixItem]()
let encoder = JSONEncoder()
let decoder = JSONDecoder()
@IBAction func restoreFrom(_ sender: Any) {
let restoredJson: Data = UserDefaults.standard.data(forKey: "storeArray")!
// its here that i can't determine how to decode the return value to my array
// error: cannot invoke decode with an argument list of type([Decodeable], from Data:)
try! JSONDecoder().decode([returnArray.self], from: restoredJson)
print("return json size \(restoredJson)")*
}
@IBAction func appendItem(_ sender: Any) {
if let newValue = newItem.text {
print(newValue)
newItem.text = " "
let itemToAdd = matrixItem(matrixName: newValue, matrixDescription: itemDescription.text!)
matrixArray.append(itemToAdd)
print(matrixArray)
let jsonData = try! encoder.encode(matrixArray)
print("Print json data \(jsonData)")
UserDefaults.standard.set(jsonData, forKey: "storeArray")
}
}