• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Bindings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bindings


  • Subject: Re: Bindings
  • From: Chris Hanson <email@hidden>
  • Date: Sat, 26 May 2007 21:44:17 -0700

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:
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>)

  • Prev by Date: Re: EXC_BAD_ACCESS in NSTableView adding object when visible
  • Next by Date: Re: EXC_BAD_ACCESS in NSTableView adding object when visible
  • Previous by thread: Re: Bindings
  • Next by thread: Re: Bindings
  • Index(es):
    • Date
    • Thread