Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions Demo/LineNumber/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}


Expand Down
21 changes: 19 additions & 2 deletions Demo/LineNumber/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,34 @@ class ViewController: NSViewController {

@IBOutlet var mainTextView: NSTextView!

var document: Document? {
return view.window?.windowController?.document as? Document
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
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.

}
}
}


}