Last active
July 14, 2017 19:16
-
-
Save bgreenlee/73bdb1967750ecbae9f7a8fbf6aa5eb6 to your computer and use it in GitHub Desktop.
Swift 3 version of Document.swift from https://snackcoffee.me/2015/08/27/osx-document-based-app-swift/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class Document: NSDocument { | |
@IBOutlet weak var textField: NSTextField! | |
var content = "" | |
override init() { | |
super.init() | |
// Add your subclass-specific initialization here. | |
} | |
override class func autosavesInPlace() -> Bool { | |
return true | |
} | |
override var windowNibName: String? { | |
// Returns the nib file name of the document | |
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this property and override -makeWindowControllers instead. | |
return "Document" | |
} | |
override func windowControllerDidLoadNib(_ windowController: NSWindowController) { | |
super.windowControllerDidLoadNib(windowController) | |
textField.stringValue = content | |
} | |
override func read(from url: URL, ofType typeName: String) throws { | |
content = try String(contentsOf: url, encoding: String.Encoding.utf8) | |
} | |
override func write(to url: URL, ofType typeName: String) throws { | |
try textField.stringValue.write(to: url, atomically: false, encoding: String.Encoding.utf8) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment