Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Last active July 14, 2017 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgreenlee/73bdb1967750ecbae9f7a8fbf6aa5eb6 to your computer and use it in GitHub Desktop.
Save bgreenlee/73bdb1967750ecbae9f7a8fbf6aa5eb6 to your computer and use it in GitHub Desktop.
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