0

I'm trying to make a small macOS viewer app that will display the content of ODT (Open Document) files but not allow editing in the way that TextEdit does. With the help of an online tutorial I've put together an RTF viewer that includes this code in Document.swift:

override func read(from data: Data, ofType typeName: String) throws {
     if let contents = NSAttributedString(rtf: data, documentAttributes: nil) {
        text = contents
    }
}

What I can't figure out is how to make the program read ODT data instead of RTF data, and I can't find any documentation that helps out. So my question is: How can I rewrite that code to read in an ODT file instead of an RTF file?

emendelson
  • 416
  • 5
  • 18
  • That's a whole format that might not be handled by default by the SDK. Look how the protocol look like (it might be like a .docx with, with xml embedded in a .zip). – Larme Jul 02 '18 at 07:55
  • Try ``init(url:options:documentAttributes:)`. – Willeke Jul 02 '18 at 11:20
  • @Willeke - That certainly looks like the right answer (it seems to be the answer to similar problems elsewhere, now that I know to search for that string), but I'm too much of a beginner to figure out exactly how to use it. If you're feeling generous, could you possibly suggest the rest of the "override func read" block? If not, I'll keep struggling with it. Meanwhile, thank you! – emendelson Jul 02 '18 at 11:50

1 Answers1

1

Override read(from url: … instead of read(from data: … and use NSAttributedString's init(url:options:documentAttributes:).

override func read(from url: URL, ofType typeName: String) throws {
    text = try NSAttributedString(url: url, options: [:], documentAttributes: nil)
}
Willeke
  • 14,578
  • 4
  • 19
  • 47