I want to encode an Array of my own struct, which I want to save in UserDefaults, and then read It out (after decoding it). I know how to code the first part (following), that means how to encode an array and save it in Userdefaults.
struct mystruct: Codable {
var int: Int
var string: String
}
var array = [mystruct(int: 2, string: "Example"), mystruct(int: 5, string: "Other Example")]
var encodedArray = try JSONEncoder().encode(array)
UserDefaults.standard.set(encodedArray, forKey: "array")
I also know how to get the data back from the Userdefaults:
var newArray = UserDefaults.standard.data(forKey: "array")
But I don't know how to decode an whole array...