Reputation: 911
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?
Upvotes: 0
Views: 549
Reputation: 911
Getting rid of the "throw" above fixed the read method, as vadian suggested.
The data method required two fixes. First, apparently nobody tells you that you now need to set permissions to be able to write a file. I had to do that, following the instructions at the link.
Second, I think I needed to drop the "self" when getting the view controller? I'm not entirely sure why this worked, but I played around with the code more and changed it to
var viewController: ViewController? {
return windowControllers[0].contentViewController as? ViewController
}
override func data(ofType typeName: String) throws -> Data {
let textView = viewController?.theTextView
if let contents = textView?.string.data(using: String.Encoding.utf8)
{
return contents
}
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
And that successfully saves.
Upvotes: 0