I'm loading a text file, the encoding is unknown as it comes from other sources. The content itself comes from macOS NSDocument's read method, which is fed into my model's read. The String constructor requires the encoding when using Data, if you assume the incorrect one you may get a null. I've created a conditional cascade of potential encodings (it's what other people seem to be doing), there's gotta be a better way to do this. Suggestions?
override func read(from data: Data, ofType typeName: String) throws {
model.read(from: data, ofType: typeName)
}
In the model:
func read(from data: Data, ofType typeName: String) {
if let text = String(data: data, encoding: .utf8) {
content = text
} else if let text = String(data: data, encoding: .macOSRoman) {
content = text
} else if let text = String(data: data, encoding: .ascii) {
content = text
} else {
content = "?????"
}
}