Re: cocoa-dev digest, Vol 2 #2462 - 14 msgs
Re: cocoa-dev digest, Vol 2 #2462 - 14 msgs
- Subject: Re: cocoa-dev digest, Vol 2 #2462 - 14 msgs
- From: Renaud Boisjoly <email@hidden>
- Date: Wed, 11 Jun 2003 11:15:13 -0400
Thanks Emma! I'll digest all this and I'm sure it will work. Greate
explanation!
Tkae care
- Renaud
On Tuesday, June 10, 2003, at 09:38 PM,
email@hidden wrote:
Date: Wed, 11 Jun 2003 09:54:07 +1000
Subject: Re: Saving state of NSOutlineView
From: Emma Whan <email@hidden>
To: email@hidden
You don't have to return a string from
outlineView:persistentObjectForItem: In fact, since the documentation
for these methods suggests you should return an archived object, that
is what I have always done. What I do is archive the item itself in
persistentObjectForItem and then unarchive it in
itemForPersistentObject. The object does have to be unique for every
item in your datasource. The process will only work if isEqual is
implemented in the objects (for reasons explained below). So if you
are archiving items of a custom class, make sure you implement isEqual
in that class first. Obviously with a custom class, you also need to
implement encodeWithCoder and initWithCoder.
The way that autosaveExpandedItems actually works is that every time
you expand an item, a copy of that item is archived in [NSUserDefaults
standarduserDefaults]. When you relaunch the application, it checks
every item in the outlineView and compares it with the ones it has
saved in the standardUserDefaults. If there is a match, it expands the
item. Because it needs to check for matches, isEqual has to be
implemented or none of your items will be expanded.
My code for implementing persistentObjectForItem and
objectForPersistentItem is simply as follows:-
- (id)outlineView:(NSOutlineView *)outlineView
persistentObjectForItem:(id)item
{
return [NSKeyedArchiver archivedDataWithRootObject:item];
}
- (id)outlineView:(NSOutlineView *)outlineView
itemForPersistentObject:(id)object
{
return [NSKeyedUnarchiver unarchiveObjectWithData:object];
}
Saving NSStrings instead of archived objects would probably work (I
haven't tried it) but each string will need to be unique and your
methods would obviously need to be able to determine what string
belongs to what object. The difficulty with using strings is that it
is quite likely that two items might return the same string. You need
to come up with some way of ensuring that each object can be uniquely
identified. This is the same even if you use archived objects so that
isEqual will only return true if the exact same object is unarchived.
I use timestamps for this purpose. Whenever I create an object in my
datasource, I give that object an NSTimeInterval variable representing
the exact submillisecond it was created. isEqual then compares the
timestamps.
I suppose I could have just returned the timestamp itself (or an
NSNumber object representing it) in the persistentObjectForItem method.
But if I did that, in the itemForPersistentObject method I would have
to loop recursively through every single item in my datasource until I
found the item with the matching timestamp.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.