Chapters 1 and 2: Model View Controller

There’s no code the first chapter, just a thorough explanation of the MVC patterns and how Cocoa uses them. The interesting tips I got were that (a) you can create an NSDocument subclass without a GUI (I’ve wanted to do that for testing, but couldn’t figure out how) and (b) you can create a pane for the System Preferences application.

The second chapter works up a pay calculator program that handles overtime pay and exempt personnel. The first version doesn’t use MVC, while the second one does. Both involve similar amounts of code, but the MVC version takes advantage of several Cocoa frameworks to add a table of employees, undo and document-based save.

The book’s explanations aren’t tutorials: if you don’t already know how to do Cocoa programming, this chapter won’t help you. If you do, they go very quickly: it took me under 20 minutes to do the non-MVC version and under 40 to do the MVC version.

I learned some Core Data tricks from the program. Go take a look at the header file for the Employee class.

Note that there are no accessors: apparently you don’t need to create them at all for a managed object class if you don’t want to. I believe that this works as long as you only access attributes via key-value coding.

Now go look at the implementation file. Note that there are only two methods!

The class method + (NSSet *)keyPathsForValuesAffectingValuesAffectingPayAmount tells
Core Data to automatically recalculate the value of payAmount whenever any of the listed keys change. The general version is + (NSSet
to do this for the
attribute VariableName. The accessor method - (NSNumber *)payAmount does the calculation.

This entry was posted in MVC. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s