Saving and Restoring document in Cocoa-Document based application

Discussion in 'Mac Programming' started by KPForMacRum, Oct 4, 2008.

  1. KPForMacRum macrumors newbie

    Joined:
    Sep 30, 2008
    #1
    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
     
  2. mobilehaathi macrumors G3

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #2
    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"];
    }
     

Share This Page