MVC - Cocoa interface - Cocoa Design pattern book

Discussion in 'Mac Programming' started by Idanr, May 20, 2010.

  1. Idanr macrumors newbie

    May 20, 2010
    So I started reading this book:

    On chapter 2 it explains about the MVC design pattern and gives and example which I need some clarification to.

    The simple example shows a view with the following fields: hourlyRate, WorkHours, Standarthours , salary.

    The example is devided into 3 parts : View - contains some text fiels and a table (the table contains a list of employees' data).

    Controller - comprised of NSArrayController class (contains an array of MyEmployee)

    Model - MyEmployee class which describes an employee. MyEmployee class has one method which return the salary according to the calculation logic, and attributes in accordance with the view UI controls. MyEmployee inherits from NSManagedObject.

    Few things i'm not sure of : 1. Inside the MyEmplpyee class implemenation file, the calculation method gets the class attributes using sentence like " [[self valueForKey:mad:"hourlyRate"] floatValue];" Howevern, inside the header there is no data member named hourlyRate or any of the view fields.

    I'm not quite sure how does it work, and how it gets the value from the right view field. (does it have to be the same name as the field name in the view). maybe the conncetion is made somehow using the Interface builder and was not shown in the book ?

    and more important: 2. how does it seperate the view from the model ? let's say ,as the book implies might happen, I decide one day to remove one of the fields in the view. as far as I understand, that means changing the way the salary method works in MyEmplpyee (cause we have one field less) , and removing one attribute from the same calss. So how is that separate the View from the Model if changing one reflect on the other ?

    I guess I get something wrong... Any comments ? Thanks
  2. rossipoo macrumors regular

    Jun 7, 2009
    I remember being a little confused here too, however it's a great book, I found it very useful overall, and learned way more than from some other books that are often recommended here.

    The reason you can call [self valueForKey:] is because you are inheriting from NSManagedObject rather than NSObject. NSManagedObject implements this functionality. You could easily make a simple version of this using an NSDictionary. NSManagedObject does a lot more though, it works with CoreData to allow it to save your data automatically, and has undo functionality built-in.

    In order for the managed object to know what values are valid, you need to add a Data Model file. After creating the headers for MyEmployee go to File -> New File... -> Resource -> Data Model and add the MyEmployee class to the Data Model. There is a figure in the book that shows what to put in the Data Model file.

    My understanding is that the Data Model is effectively part of your application model, so changes will count as changes to the model. Your interface can be changed however you like though, without having to change the Data Model.

Share This Page