Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bindings



On May 26, 2007, at 7:36 PM, I. Savant wrote:

Sandro:

[_bServices addObject:aNetService];

What is _bServices? If it's anything but your controller, you're "changing the array behind the controller's back". You must either:


a) Call -willChangeValueForKey: and -didChangeValueForKey: before/ after changing the array, or,

b) Call -addObject: against your array controller.

You really should not do (b) from model-level code, because you should not be using controller objects to manipulate your model objects.


Instead, send -mutableArrayValueForKey: or -mutableSetValueForKey: to a model object that has a to-many relationship which you want to change. The result of those messages will be an NSMutableArray or NSMutableSet that you can manipulate which will ultimately modify the appropriate relationship of your object, and will ensure that appropriate KVO notifications are posted.

While the standard Xcode templates don't create separate "Model," "View," and "Controller" folders in new projects, you should still remember to partition your application along those lines. Cocoa was Opinionated Software[1] before Ruby on Rails was a gleam in DHH's eye; it's designed to help you build software that follows a Model-View- Controller architecture. If you stray from that by working across layers and so on, you'll start going against the grain of the framework and things will become more difficult and confusing.

(A good example of the latter is that people who *do* try to use (b) above to manipulate relationships at the model level get confused when they try to debug their code. A distressingly common question on the list is, "I sent -addObject: to my controller, why didn't the collection it's bound to change immediately?")

  -- Chris

[1] http://gettingreal.37signals.com/ch04_Make_Opinionated_Software.php

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Bindings (From: Sandro Noel <email@hidden>)
 >Re: Bindings (From: Chris Hanson <email@hidden>)
 >Re: Bindings (From: Sandro Noel <email@hidden>)
 >Re: Bindings (From: "I. Savant" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.