Thursday, August 02, 2007

Need Help Understanding MVC Implementation

And now I get to find out if I have any readers. (oh please! oh please!)

It seems that every time I think I understand something, I conduct a test of this understanding, and the test fails. At a high level, I believe that I do understand MVC. However, upon sitting down to actually implement something, I wind up having to muddy the distinction between M/V/C classes in order to make the darned thing work. Case in point is the mileage program I'm working on. I have a GasTank class. No question, this is a model class. And then I have an interface, clearly view classes. And then I have an AppController, clearly a contro...oops, I have an NSMutableArray of ManyGasTanks in there. Not to mention a bunch of methods for working with that array. Hrm. The more I read about MVC theory, the more I feel that my array is in the wrong place. I guess I should have some kind of AllGasTanks class, and then make AppController talk to it.

Before I hork my mostly-working program in the interest of theoretical purity, I decide to conduct an experiment. I don't think I've made a controller try to talk to an object whose data content is an array yet, so I need to try something simple. Let's try a rainbow. So I have a Color class (model). I have a Rainbow class (model) that will be an array of Colors. I have an NSTableView (view) that will simply display the list of colors. And then I have an AppController (controller). I'll populate the array programatically, so I'm not going to bother with buttons to add or delete for right now. At this point in the experiment, I think I understand this; it makes sense to me.

However, I have not yet succeeded in getting the table to display anything. I'm trying not to use bindings because they don't help me to understand what is going on, but I can't seem to find the right sequence of events to make them work either. Without wasting too much effort posting source code here (although I can certainly do so if it will help), I'll try to form some coherent questions, but I'm really lost.

1) Is this even worth worrying about? I'm pretty sure I could make this work with a Color class and then a RainbowController. Should I be beating myself up over the fact that I don't have a Color class, a Rainbow class, and an AppController class?

2) Let's assume that I do indeed have a Rainbow class. What methods should it have? At a lower level, I think I get instance variables (foo). And then I think I get getter/setter methods (foo/setFoo). But now I have a class whose purpose is to be an array. Thinking ahead to the dataSource methods that my AppController will have to implement, how do I extract information from the array inside the object? I don't seem to have much luck with [Rainbow count] - warning: 'Rainbow' may not respond to '+count.' I'll buy that. So AppController needs to create an instance of Rainbow, and I'll sent the count message to it. Oops...warning: 'Rainbow' may not respond to '-count.' I can't imagine that I have to re-implement all of the NSMutableArray methods in my Rainbow class, so there must be another way. I am really missing something. Also, if I create an instance of Rainbow in AppController, say in (id)init, I get a warning saying that the instance name I have created is an unused variable. I encountered this in my mileage program, too, even though it was used in other methods. As far as I can tell, the warning was only looking within the context of (id)init, which doesn't make sense to me because I've seen other examples doing exactly what I'm doing and they work fine. Either I don't understand why I should NOT have a Rainbow class, or I don't understand how to set up Rainbow so that AppController can talk to it. And/or both. Or maybe Rainbow should be a subclass of something other than NSObject?

3) Should a "pure" model class ever appear in the NIB file? AppController would be the delegate of the NSTableView. Should I drag in an instance of Rainbow to be the dataSource? Something about putting a model class into the NIB file doesn't feel right to me.

4) I did try using bindings. I drag in an NSArrayController. I set the Object Class Name to Rainbow. I get fuzzy after that. I don't really understand what the Controller Key and/or Model Key Path need to be in this case. I tried variations on a number of things for either or both fields, with no luck. My lack of understanding is doubtless leading to my lack of success.

I am in dire need of a big picture here. And maybe an example or two. Can anyone help me out?

No comments: