On Jun 29, 2008, at 10:44 AM, Stuart Malin wrote:


On Jun 29, 2008, at 7:05 AM, Scot t Anguish wrote:

From: Scott Anguish <[EMAIL PROTECTED]>
Subject: Re: mutableArrayForKeyValue Question (ignore other email)
To: "Alex Wait" <[EMAIL PROTECTED]>
Cc: cocoa-dev <Cocoa-dev@lists.apple.com>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

I think there is still some clarification necessary.

what exactly are you trying to accomplish, rather than how you are
trying to do it?

would I be correct to guess that you want to create a new Person
object and add it to the array?

if so you need to create the object and add it to the array

id newObjectToAdd = [theArrayController newObject];
[theArrayController addObject:newObjectToAdd];


or you can programmatically create the Person object, and add it
directly to the datamodel using, say

Person *aPerson=[[[Person alloc] init] autorelease];
and then add that to the data model using the mutableArrayforKeyValue:


As I am playing around with the very same concepts now, I tried these two approaches in my code, and get (slightly) different results.

When using the controller to add the Person object, the added object is NOT initialized. My Person class's -init method sets initial values for its ivar properties. When I create an explicit instance of aPerson, and add that to the model, the values are shown in the view(s). However, when I use the controller to add a new object, the ivars are not initialized.

This is surprising. Is Person an NSManagedObject subclass? If it were a plain old NSObject subclass, the controller creates new instances by invoking [[<class> alloc] init].

I'm not sure how else you'd end up with an object at all if your init method weren't getting called.

If Person is an NSManagedObject subclass, init isn't the designated initializer. In this case, the controller creates the new object using the initWithEntity:inManagedObjectContext: or whatever the NSManagedObject designated initializer is.

If you've put setup logic in your NSManagedObject subclass's init method, you've put the logic in the wrong place.



Am I doing something wrong? Or is this is known artifact of using the controller as an intermediary to add an new instance?

~~~

Separately, there's another way to add an explicitly created Person to the data model -- using the index accessors, such as: [mutableArrayModel insertObject:aPerson in<Key>AtIndex: [mutableArrayModel countOf<Key>]];

Actually, in my case, I made an instance method in the model class that adds a new person: -(void) add<Key>:(Person*)aPerson that invokes the above referenced index accessors on self to add the new person to the <Key> property.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/luesang%40apple.com

This email sent to [EMAIL PROTECTED]


--------------------------
RONZILLA



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to