diff --git a/Demo/LineNumber/Document.swift b/Demo/LineNumber/Document.swift index 2718169..d719288 100644 --- a/Demo/LineNumber/Document.swift +++ b/Demo/LineNumber/Document.swift @@ -27,6 +27,9 @@ import Cocoa class Document: NSDocument { + var viewController : ViewController? + var documentContent : String? + override init() { super.init() // Add your subclass-specific initialization here. @@ -50,17 +53,27 @@ class Document: NSDocument { self.addWindowController(windowController) } + // more information about what's happening here can be found at + // http://sketchytech.blogspot.com/2016/09/taming-nsdocument-and-understanding.html override func data(ofType typeName: String) throws -> Data { - // Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil. - // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. - throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) + // since NSDocument returns a non-optional, + // we should return something, even if it's nothing + var data = Data() + + if let rawString = self.viewController?.mainTextView.string + { + if let rawData = rawString.data(using: .utf8) + { + data = rawData + } + } else { + throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) + } + return data } override func read(from data: Data, ofType typeName: String) throws { - // Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false. - // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead. - // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded. - throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) + documentContent = String(decoding: data, as: UTF8.self) } diff --git a/Demo/LineNumber/ViewController.swift b/Demo/LineNumber/ViewController.swift index f0c327c..debd00d 100644 --- a/Demo/LineNumber/ViewController.swift +++ b/Demo/LineNumber/ViewController.swift @@ -29,6 +29,10 @@ class ViewController: NSViewController { @IBOutlet var mainTextView: NSTextView! + var document: Document? { + return view.window?.windowController?.document as? Document + } + override func viewDidLoad() { super.viewDidLoad() @@ -36,10 +40,23 @@ class ViewController: NSViewController { mainTextView.lnv_setUpLineNumberView() } + override func viewWillAppear() { + super.viewWillAppear() + + if let document = self.document { + document.viewController = self + + if let documentContents = document.documentContent { + self.mainTextView.string = documentContents + } + } + } + override var representedObject: Any? { didSet { // Update the view, if already loaded. - } - } + } + + }