Re: Problem With implementing copyWithZone
Re: Problem With implementing copyWithZone
- Subject: Re: Problem With implementing copyWithZone
- From: Thaddeus Cooper <email@hidden>
- Date: Sun, 7 Oct 2007 20:16:23 -0700
Chris thanks for the great information.
If I understand -initWithEntity:insertIntoManagedObjectContext: then
it will create an object in the store -- which I don't believe I
want. I am only implementing copyWithZone: because it is required
for the composite NSCell subclass that I am working on which combines
multiple pieces of data from an NSManagedObject subclass into a
single cell, so I believe that I want an absolute duplicate of what
is already in the store.
A little background. What I am trying to do is create a cell that is
used in the column of a table to show multiple pieces of data from an
NSManagedObject so that the user has a high level overview of the
information without having to plow through a lot of columns. So I
tried using an NSTextFieldCell as the class from which I subclassed
my new class because the data is mostly text with some icons, but the
data returned by objectValue was the description string of the
NSManagedObject (using the binding arrangedObjects.self) which isn't
what I wanted (I want the actual NSMangedObject), so as an experiment
I decided to use NSCell instead, but that requires copyWithZone: to
be implemented. And here I am. :-(
So either the way I am binding the cell in the table view is wrong,
or I am thinking about the NS[TextField]Cell subclass incorrectly. I
know I'm not the first one down this path, but I have not been able
to find much documentation about this, so If you or anyone else has a
suggestion as to how to proceed once again I'd be really grateful.
Thanks very much.
Thaddeus O. Cooper
(email@hidden)
On Oct 7, 2007, at 7:47 PM, Chris Hanson wrote:
On Oct 7, 2007, at 6:25 PM, Thaddeus Cooper wrote:
My implementation looks like:
- (id) copyWithZone:(NSZone *)zone {
id copiedWine;
copiedWine = [[[self class] allocWithZone:zone] init];
The designated initializer for NSManagedObject is -
initWithEntity:insertIntoManagedObjectContext: and not -init as
your code assumes. This means that your instance doesn't know what
its entity is, and therefore doesn't know what properties it has,
which leads to this exception:
2007-10-07 18:12:40.205 WineCellar[5988:10b] *** Terminating app
due to uncaught exception 'NSUnknownKeyException', reason: '[<Wine
0x174027e0> setValue:forUndefinedKey:]: the entity (null) is not
key value coding-compliant for the key vineyard.'
Also, you have to be extremely careful in determining what,
exactly, it means to "copy" an instance of your entity. Does it
mean to create a new, identical instance of that entity with both
the same attributes *and* the same relationships? What if your
entity has relationships with to-one inverses? Do you copy the
entire object graph rooted at that object? If so, what if the
object graph isn't strictly hierarchical?
-- Chris
_______________________________________________
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