need help understanding how Document class fits into MVVM

I have a fairly simple document app with a tableView... using MVVM... can't figure out how to get my data to the Document class so that it can be saved / loaded. I'm not listing the Document class here because it's the exact boilerplate that comes with a document app. As you can see right now my data is saved in the ViewModel... the array called "notes." That's what I want to save... perhaps by using JSON or maybe NSKeyedArchiver, not sure what's best. I just don't know how to have access from the Document class to the notes array. I've looked a little at representedObject but it's so confusing... any help would be much appreciated.

Model:

Code Block
import Foundation
struct Note: Codable {
   
  var timecodeIn: String
  var timecodeOut: String
  var note: String
  var comment: String
   
  init(timecodeIn: String, timecodeOut: String, note: String, comment: String) {
    self.timecodeIn = timecodeIn
    self.timecodeOut = timecodeOut
    self.note = note
    self.comment = comment
  }
}


View Model:
Code Block
import Foundation
class ViewModel: NSObject, Codable {
   
  // MARK: - Properties
   
  var notes = [Note]() //this ultimately is the array I want saved. Maybe it doesn't go here?
   
  // MARK: - Init
   
  override init() {
    super.init()
  }
   
  // MARK: - Public Methods
 
  //set note
  func setNote(note: Note) -> Void {
    notes.append(note)
  }
   
   
  //delete 1 note
  func deleteNote(atIndex index: Int) {
    notes.remove(at: index)
  }
   
  //delete <1 notes
  func deleteNotes(atIndexSet set: IndexSet) {
    var count = 0
    for index in set {
      notes.remove(at: (index - count))
      count += 1 //funky, but it works!
    }
  }
}

View Controller:
Code Block
import Cocoa
class ViewController: NSViewController {
   
  // MARK: - IBOutlet Properties
   ...
  // MARK: - Properties
   
  var viewModel = ViewModel()
   
  // MARK: - View Controller Lifecycle
   
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
  }
  override var representedObject: Any? {
    didSet {
       
    }
  }
   
  override func viewWillAppear() {
    super.viewWillAppear()
    tableView.reloadData()
  }
   
   
  // MARK: - IBAction Methods
   
  @IBAction func addNote(_ sender: NSButton)
  {
    ...
    viewModel.setNote(note: tempNote)
    tableView.reloadData()
  }
   
  @IBAction func removeNote(_ sender: NSButton)
  {
    ...
      viewModel.deleteNotes(atIndexSet: tableView.selectedRowIndexes)
    
  }
}
  // MARK: - ViewController extensions
extension ViewController: NSTableViewDataSource {
  func numberOfRows(in tableView: NSTableView) -> Int {
    return viewModel.notes.count
  }
}
extension ViewController: NSTableViewDelegate {
   ...
}


Replies

Well, after wrangling with this Document file for a couple days, I've come to the conclusion that the best approach is to toss the thing out, and just write my own code for open, save, save as... etc. functions.

Plus 1 for Apple trying to make things easier, but instead making them damn near impossible. By "hiding" the code in a document based app that does the actual write(to: and Data(contents of:, it makes it impossible to tweak the code to work for your particular application. Not everyone wants to create another run of the mill document app. But if you can't see how the file is getting saved, how the URL is being pulled from the savePanel, how the data is being loaded from the file... you can't alter the code to suit your purposes. Luckily I was able to hodgepodge something together from a variety of internet tutorials, written by people who haven't completely given up on macOS... :[