I did some Swift programming a while ago in Swift 2 and am now trying to use Swift 4. Maybe I'm missing something really obvious, but I cannot for the life of me get an extremely simple document-based text-editor application to open or save files properly. I make the document-based application and add this code to the ViewController class:
@IBOutlet var theTextView: NSTextView!
Then I go to the storyboard, add a Text View to it, and connect that Text View to theTextView as an outlet. I added data and read functions to Document as follows:
override func data(ofType typeName: String) throws -> Data {
if let vc = self.windowControllers[0].contentViewController as? ViewController {
return vc.theTextView.string.data(using: String.Encoding.utf8) ?? Data()
}
else {
return Data()
}
}
override func read(from data: Data, ofType typeName: String) throws {
if let s = String(data: data, encoding: String.Encoding.utf8) {
string = s
}
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
The program compiles and runs. But whenever I try to save, no save dialogue comes up and the app becomes unable to quit (I have to stop it from within Xcode). Whenever I try to open a file in the format I set for the app (even if it's just .txt), I get the error "The document [filename] could not be opened." I get that exact same behaviour even when all I do is add the TextView to the view controller, with no outlets or code added. So clearly Cocoa is not recognizing my code and/or outlets as relevant, but I cannot for the life of me figure out why. What am I missing?