More Problems with archiving.
More Problems with archiving.
- Subject: More Problems with archiving.
- From: Development <email@hidden>
- Date: Tue, 4 Dec 2007 16:10:40 -0700
I cannot seem to make keyed archiving work and I dont know why. For
the outline view I use a simple leaf node structure. I am trying to
archive and then retrieve that structure so in the nodeitem object
I've implemented NSCoding and am encoding it as follows:
-(void)encodeWithCoder:(NSCoder*)coder
{
NSLog(@"Encoding root item: %@",self);
[coder encodeObject:itemParent forKey:@"ItemParent"];
[coder encodeObject:itemName forKey:@"ItemName"];
[coder encodeObject:itemType forKey:@"ItemType"];
[coder encodeObject:children forKey:@"ItemChildren"];
[coder encodeObject:itemProperties forKey:@"ItemProperties"];
[coder encodeObject:itemPath forKey:@"ItemPath"];
[coder encodeObject:itemNote forKey:@"ItemNote"];
[coder encodeBool:isGroup forKey:@"Expandable"];
}
I can see when the file is created that this encoding method is in
fact being called. And when I look in the data file I can see all the
keys there.
However when I attempt to retrieve the data, no matter how I do it,
it's null. In the NSDocument subclass that I use to hold the
information about this structure is have implemented:
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:
(NSError **)outError
{
if([typeName isEqualTo:@"My Project"]){
NSLog(@"Opening a project File");
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver
alloc]initForReadingWithData:data];
projectDict = [[NSMutableDictionary alloc]initWithDictionary:
[[unarchiver decodeObjectForKey:@"ProjectDict"]retain]];
projectStruct = [[NSMutableArray alloc]initWithArray:[[unarchiver
decodeObjectForKey:@"ProjectStruct"]retain]];
NSData * nodeData = [[unarchiver
decodeObjectForKey:@"RootNode"]retain];
NSKeyedUnarchiver * newArch = [[NSKeyedUnarchiver
alloc]initForReadingWithData:nodeData];
//I get an error here that the data object is empty.
_root = [[NodeItem alloc]initWithCoder:newArch];
NSLog(@"unarchived: %@, %@, %@",_root,projectDict,projectStruct);
//Here an empty array and empty dictionary print and where it should
print the root's name it prints null.
//I have also tried a straight _root = [[unarchiver
decodeObjectForKey:@"RootNode"]retain]
return YES;
}
return NO;
}
The dictionary and array that I encode when creating the project file
appear to decode correctly however the NSObject subclass that is the
NodeItem just come out null and I cannot seem to find any possible way
to get it to actually read the information.
Finally when the actual document is encoded for writing this is what
is called":
-(void)encodeWithCoder:(NSCoder*)coder
{
NSLog(@"CAlling encode with coder %@",coder);
[coder encodeObject:projectDict forKey:@"ProjectDict"];
[coder encodeObject:projectStruct forKey:@"ProjectStruct"];
NSData * nodes = [NSKeyedArchiver archivedDataWithRootObject:_root];
// I've also tried this with a flat [coder encodeObject:_root
forKey:@"RootNode"];
NSLog(@"Encoding Data: %@",nodes);
[coder encodeObject:nodes forKey:@"RootNode"];
NSLog(@"Reached end of encode");
}
_______________________________________________
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