Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

KPForMacRum

macrumors newbie
Original poster
Sep 30, 2008
7
0
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:mad:"properties"]];
[self setEmails: [coder decodeObjectForKey:mad:"emails"]];
}
return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: properties forKey:mad:"properties"];
[coder encodeObject: emails forKey:mad:"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
 
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.:)

Code:
// 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"];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.