Flush from my unexpected success with the “junk data” coming from my calculator class, I decided to step back and take care of some of the boring bits: document opening and saving, undo, printing, copy and paste, and drag and drop. This is the stuff that people expect to be able to do with any Mac application, so I have to make sure it’s there!

I started with saving. Maybe because it seems so simple in the Hillegass book, I thought this wouldn’t be a problem. Ha ha. I’ve officially hit The Wall.

The Wall is a place I expect to be sitting against an awful lot during this process. Where my understanding comes smack up against my ignorance… and Ignorance is way bigger, and it ain’t movin’. Implementing the initial bit of functionality proved to be relatively easy, and I’ve been really surprised at myself. But this… this is crap.

I should confess, the last few days have been difficult. I may be coming down with something my daughter brought home from school, because I’ve had a hard time getting up in the morning these last few days, and I haven’t been very productive at all in front of my Mac. I managed to make it to the gym today, but I’m definitely in a funk. Which makes the Wall even bigger.

We’ll get past that. For now, let’s dissect my issue for posterity’s sake.

First Attempt: NSCoder Going along with Hillegass, I thought the way to go was to use the NSCoding protocol on my model class, encode the data and then have NSDocument’s dataOfType:error: method magically convert that into an archived file on my disk using NSKeyedArchiver.

I had nothing but trouble trying to implement this scheme. As it turns out, my grasp of Model-View-Controller is quite weak, and I initially identified my NSTextView subclass as the place to implement NSCoding. Then I realized that it’s really a View class, and went back to MyDocument to implement it there — NSDocument is actually a hybrid model-controller. Or something.

The net result: documents would apparently save, and a file would appear on my Desktop, but when I open it in the Napkin app, I get a blank document. That ain’t right…

Second Attempt: dataOfType:error: I asked my friend Jim Dovey if he could point me in the right direction. Jim’s a freakin’ Cocoa Superstar, which explains why he doesn’t appear on iChat too often! He pointed out that NSCoding was a little overkill for this situation, and that I should go straight to the NSDocument methods that come with my NSDocument template: dataOfType:error and readFromData:ofType:error. He also said something about having to override another method, but I’m only going on memory because stupid iChat doesn’t keep transcripts as I assumed it would!

Fortunately, the documentation for dataOfType:error: is somewhat helpful:

The default implementation of this method throws an exception because at least one of the writing methods (this method, writeToURL:ofType:error:, fileWrapperOfType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error:) must be overridden.

But which one? And to do what? Gahhh! I’ve been tooling with this for a while, obviously, and I don’t have an answer yet.

Thus far, my understanding is that dataOfType:error gets called after you hit Save… and choose a file location and name. My dataOfType:error method creates an NSData object, puts the contents of my NSTextView into it, and…

Yeah. What happens next? Nothing! Just like with the first attempt, I end up with an empty document on my Desktop.

So clearly, my next step is to dick around with overriding one of those methods. But that’ll have to come later; I’m feeling a bit drained.