Re: How to archive structs like NSRect
Re: How to archive structs like NSRect
- Subject: Re: How to archive structs like NSRect
- From: David Spooner <email@hidden>
- Date: Mon, 12 Nov 2007 22:29:25 -0700
I had sent this privately earlier, but it may be of interest generally.
You can get around the problems with keyed archiving of NSValues by
using a coding proxy along the lines of...
@implementation MyValueKeyedCodingProxy : NSObject
{ NSData *data; }
- (id) initWithValue:(NSValue *)aValue
{ data = [NSArchiver archivedDataWithRootObject:aValue]; return
self; }
- (id) initWithCoder:(NSCoder *)coder
{ data = [coder decodeObject]; return self; }
- (void) encodeWithCoder:(NSCoder *)coder
{ [coder encodeObject:data]; }
- (id) awakeAfterUsingCoder:(NSCoder *)coder
{ return [NSUnarchiver unarchiveObjectWithData:data]; }
@end
@implementation NSValue(MyKeyedArchivingWorkaround)
- (id) replacementObjectForKeyedArchiver:(NSKeyedArchiver *)archiver
{ return [[MyValueKeyedCodingProxy alloc] initWithValue:self]; }
@end
This is written hastily and expects GC, but you get the idea...
dave
On 12-Nov-07, at 3:50 PM, Gordon Apple wrote:
It's good to know this exists. I had never seen this. However, it
still doesn't help in encoding an NSDictionary. You have to put these
things into an NSValue just to get them into the dictionary. Then
if you
want to encode the dictionary, you have to iterate through the
dictionary
and test every entry for every particular type of NSValue to encode
it.
Then, I'm still not sure how to decode it unless you encode Type
info with
it.
In my case, I have more than one NSDictionary, each of which
hasn't an a
priori clue as to what it contains. The keys and objects/values are
filled
in from elsewhere. Everything contained conforms to NSCoding. What
I would
like is to be able to just tell it to archive/unarchive the
dictionary and
be done with it.
_______________________________________________
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