views:

65

answers:

1

So I started reading this book: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022

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:@"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

A: 
  1. The valueForKey: method implementation is discussed here. Note that valueForKey: can actually access the ivars directly without calling any methods.

  2. If you remove a column from your NSTableView you don't have to remove it from your model object class. It's still there, it just doesn't get displayed.

Tom Dalling
Hey,Regarding 2 :But if I remove a field it means there is no data for that field anymore... so if inside the model's class's method , I refer to that attribute using valueForKey , I will get an empty variable or other undefined outcome.So ? how do u solve that ?
Idan
If you remove a column/field from a table view, the data is still in your data model object ... the table view is only for displaying. Think of it like a "headless" server. You've got a Mac Mini serving web pages. What happens when you remove the monitor? Nothing special, you just can't *see* what's happening. Remove a table column and the data model remains as it was. You just can't see it.
Greg Combs
I should add that all of this is assuming you're removing that table field at the design stage. Perhaps you're really asking about giving the user a feature whereby they select an item in a table view and click "remove" or something. (like deleting a contact from the address book). This is where you would likely grab that removal order from the table view and pass it on to your core data back end, and tell it to remove the related object from the database.
Greg Combs
Hey greg, thanks for your comments.And yes, I was referring to the second scenario.In that case, I would have no other option but changing the method's implementaion (Inside the Model) which uses that field, right ?
Idan