Re: Encoding a pointer to an object, not the object itself
Re: Encoding a pointer to an object, not the object itself
- Subject: Re: Encoding a pointer to an object, not the object itself
- From: Scott Stevenson <email@hidden>
- Date: Sun, 28 Nov 2004 12:23:00 -0800
On Nov 28, 2004, at 9:56 AM, Keith Blount wrote:
- (void)setParentFolder:(Folder *)folder
{
parentFolder = folder;
}
It is not retained or anything, as it doesn't require
it. My problem is, how do I encode and decode a
pointer like this in initWithCoder: and
encodeWithCoder:?
I have to archive and unarchive whenever I drag items
in the view, and since adding this instance variable,
things have gone horribly wrong. If I don't retain the
folder when I decode, the app crashes whenever I drag
It's a bit hard to generalize without knowing the overall structure of
the project. However, what I'd suggest is that you find a way to use
setParentFolder after unarchiving. Weak references work better if
they're populated "on the fly."
In other words, you might have something like this in your
MyController/MyDocument (untested code):
- (Folder *) parentFolderOfDocument: (Document *)theDocument (
{
NSArray *childDocuments;
id oneFolder, e = [[self folders] objectEnumerator];
while (oneFolder = [e nextObject])
{
childDocuments = [oneFolder documents];
if ([childDocuments containsObject: theDocument])
{
return oneFolder;
}
}
return nil;
}
You'd use this after unarchiving by doing something like this:
[unarchivedDocument setParentFolder: [self parentFolderOfDocument:
unarchivedDocument]];
You could possibly speed up the search in the while loop by keeping an
NSSet cache of the contained documents in the Folder object (in
addition to the normal array). An NSSet is able to tell you if it
contains a particular object more quickly. It might also help to
implement -isEqual: and -hash.
Hope that helps.
- Scott
--
http://treehouseideas.com/
http://theobroma.treehouseideas.com/ [blog]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden