So I've been trying to write my own small app starting from the basics of one of the existing projects. Specifically, I'm doing something for recording event occurrences and displaying them as a table, using Codable to store the records.
Per Project UIKit Project 12's example, I set up a basic EventRecord
class and conformed it to Codable
, as follows:
class EventRecord: NSObject, Codable {
var eventDescription: String
var occurrences: Int
var notes: String
init(eventDescription: String, occurrences: Int, notes: String) {
self.eventDescription = eventDescription
self.occurrences = occurrences
self.notes = notes
}
}
In the main TableViewController I've got the following included at the end of viewDidLoad()
:
let defaults = UserDefaults.standard
if let savedEntries = defaults.object(forKey: "entries") as? EventRecord {
let jsonDecoder = JSONDecoder()
do {
entries = try jsonDecoder.decode([EventRecord].self, from: savedEntries)
} catch {
print("Failed to load entries")
}
}
The try
line is being flagged with a fatal error, reading as follows: "Cannot convert value of type 'EventRecord' to expected argument type 'Data'".
I've spent over 4 hours tinkering with the code, comparing against the example in the book to see if I missed anything, and looking up additional information online, but I can't figure out what I'm doing that's any different from the example, which worked when I was working through it previously. All of the information is in basic types except for the conformed class itself, and when compared to the original sample code the only thing that's really different is the class name. So why isn't it working?