Skip navigation.
 
mlRe: Encoding a pointer to an object, not the object itself
FROM : Scott Stevenson
DATE : Sun Nov 28 21:23:00 2004

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]

Related mailsAuthorDate
mlEncoding a pointer to an object, not the object itself Keith Blount Nov 28, 18:56
mlRe: Encoding a pointer to an object, not the object itself Will Mason Nov 28, 19:35
mlRe: Encoding a pointer to an object, not the object itself Scott Stevenson Nov 28, 21:23
mlRe: Encoding a pointer to an object, not the object itself Keith Blount Nov 29, 00:24