Re: Initial Object in Array Controller
Re: Initial Object in Array Controller
- Subject: Re: Initial Object in Array Controller
- From: Quincey Morris <email@hidden>
- Date: Thu, 29 Sep 2011 03:03:38 -0700
On Sep 29, 2011, at 02:42 , David Mirabito wrote:
> My understanding is that because of 'preparesContent=YES' it manages the array internally, which i get at with the [myController content].
Not quite so. Array controllers *always* manage a (separate) array, or an array property if configured via bindings. That's because it's a *controller*, while the array is a data model (respectively, the C and M of the MVC design pattern). The two things have different roles, so are always distinct.
'preparesContent=YES' is an option that tells the array controller that it is responsible for creating the data model. When specified with NSObjectController (NSArrayController's superclass), it creates an object of the class configured into the controller. When specified with NSArrayController, it creates an array for you, and (as you've seen) populates it with a single object of the configured class.
> The only odd thing is that there is always a single, default item there when a new document is created.
> So apparently the NIB loading machinery is creating a new expense object and placing it into the ArrayController's content? Why would this be so? The object behaves just like any other, in that I can edit it in the tableview and/or remove via a button wired to [myArrayController remove:].
>
> Ideally the array should be completely empty in a new document, and is up to the user's interaction to place the first object. What must I do to avoid this auto-creation of the object?
Just specify 'preparesContent=NO'. Unless you have an application where it would make sense for a new document to start off with a single generic array element (and I'm having trouble thinking of an example), you don't want or need this option to be set.
There are a couple of cases where the option can *perhaps* be useful. One is with a NSObjectController, which by default gets a class of NSMutableDictionary in IB. That is, you can actually use an object controller to manage a collection of objects kept in a dictionary. (This is different from using a NSDictionaryController.) In that case, it might save you a step to have the controller create the content object (dictionary) for you.
The other case is when the NSController (of any subclass) is in Entity mode for Core Data. In that case, the option causes a default fetch, and that might save you a slightly larger step that you'd otherwise have to code manually.
In general, though, it's sort of easier to ignore the "preparesContent" option, and just take responsibility for creating the content object yourself.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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