PDA

View Full Version : Saving and Restoring document in Cocoa-Document based application




KPForMacRum
Oct 4, 2008, 09:27 PM
I am trying to convert MailDemo application to cocoa-Document based application, from scratch, and implemented the following two methods, as suggested by Apple documentation:

// Write data to a file when save is selected
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{

// Make sure we have data to save, and not just an empty document.
NSLog(@"mailboxes desctiption: %@ items.\n", self.mailboxes);
return [NSKeyedArchiver archivedDataWithRootObject:[self mailboxes]] ;

}

read data from a file, when Open is selected
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{

self.mailboxes = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"mailboxes desctiption: %@ items.\n", self.mailboxes);

return YES;

}

- (id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
[self setProperties: [coder decodeObjectForKey:@"properties"]];
[self setEmails: [coder decodeObjectForKey:@"emails"]];
}
return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: properties forKey:@"properties"];
[coder encodeObject: emails forKey:@"emails"];
}

and similar one for emails.

Everything seems fine to me, except that I can save but cannot restore the data. I can see that mailboxes array has values when trying to read from a file.

Any idea what may be happening? Thank you all great folks for your help.
KP



mobilehaathi
Oct 4, 2008, 11:47 PM
I'm not sure how to help you, but I'm just copy/pasting your code so that its formatted in code tags such that people can read it.:)

// Write data to a file when save is selected
- (NSData *)dataOfTypeNSString *)typeName errorNSError **)outError
{

// Make sure we have data to save, and not just an empty document.
NSLog(@"mailboxes desctiption: %@ items.\n", self.mailboxes);
return [NSKeyedArchiver archivedDataWithRootObject:[self mailboxes]] ;

}

read data from a file, when Open is selected
- (BOOL)readFromDataNSData *)data ofTypeNSString *)typeName errorNSError **)outError
{

self.mailboxes = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"mailboxes desctiption: %@ items.\n", self.mailboxes);

return YES;

}

- (id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
[self setProperties: [coder decodeObjectForKey:@"properties"]];
[self setEmails: [coder decodeObjectForKey:@"emails"]];
}
return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: properties forKey:@"properties"];
[coder encodeObject: emails forKey:@"emails"];
}