No idea why this error occurred, and I suspect that it's just me putting initialization in the wrong place. I've checked this post but didn't help. I can't figure out why.
struct TodosDocument: FileDocument {
...
var defaultText: String = defaultContent
var content: String
...
init(content: String = defaultContent, defaultText: String = defaultContent) {
self.defaultText = defaultText
self.content = content
do {
self.todo = try JSONDecoder().decode(Context.self, from: content.data(using: .utf8)!)
} catch {
print("ERROR \(error)")
fatalError()
}
}
static var readableContentTypes: [UTType] { [.TodoType] }
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
self.content = string
do {
self.todo = try JSONDecoder().decode(Context.self, from: data)
} catch {
self.todo = try JSONDecoder().decode(Context.self, from: defaultText.data(using: .utf8)!) // <- here
}
}
...