1

I am having an issue where I am saving, among other things, an array of "nodes" (header below) that posses a color and a tag property. When it saves as in - (void)saveDataToDiskWithPath:(NSURL*)path, the log shows that these properties exist. When I load the array again at a later time from the saved file, the frame of the node persists, but each node in the array has lost its tag and color...any ideas why? What could I be doing wrong? Maybe I'm just tired, and it will all make sense later...

Node.h:

//
//  Node.h
//

#import <Cocoa/Cocoa.h>


@interface Node : NSView {
@private
    NSColor *thisColor;
    NSColor *originalColor;
    NSRect dragRect;
    BOOL msDn;
    long tag;
    BOOL amSelected;
}

@property long tag;
@property (nonatomic, retain) NSColor* originalColor;

- (id)initWithFrame:(NSRect)frame andColor:(NSColor*)color;
- (void)select:(BOOL)yesOrNo;

@end

The save method:

- (void) saveDataToDiskWithPath:(NSURL*)path
{
    NSLog(@"Path is %@", [path absoluteString]);
    NSMutableDictionary * rootObject;
    rootObject = [NSMutableDictionary dictionary];
    for(Node* node in [sharedValues nodes]){
        NSLog(@"Node has color %@ and tag %ld.", [node originalColor], [node tag]);
    }
    [rootObject setValue: [imageView aPath] forKey:@"path"];
    [rootObject setValue:[sharedValues nodes] forKey:@"nodes"];
    [NSKeyedArchiver archiveRootObject: rootObject toFile: [path path]];
}

The load method:

- (void)loadDataFromDiskWithPath:(NSURL*)path
{
    NSDictionary * rootObject;

    rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[path path]];    
    [imageView setAPath:[rootObject valueForKey:@"path"]];
    [sharedValues setNodes:[rootObject valueForKey:@"nodes"]];
    [imageView setNeedsDisplay:YES];
}
diatrevolo
  • 2,782
  • 26
  • 45

1 Answers1

2

You will have to adopt the NSCoding protocol and implement -initWithCoder: and -encodeWithCoder: methods for the object to archive properly. Since Node is a subclass of UIView which adopts the protocol, some parts of it are being archived correctly. These are the properties of UIView and its parent classes. To ensure proper archiving, Node will have to implement the protocol too. Go through this doc for additional insight.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105