0

A Cocoa newbie here. I was trying to encode an instance variable inside an NSView to save to a file. But whenever I encode it, it is being overwritten to (null) when the initWithFrame: is being called. Is there a way I can skip this behaviour and load the instance variables on to an unarchived NSView ? Here is the code I have :

#import "Tragic.h"

@implementation Tragic {
    NSColor *color;
}


- (id)initWithFrame:(NSRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"%@",color);
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    if(!color) {
        color = [NSColor greenColor];
    }

    [color set];
    color = [NSColor yellowColor];
    NSRectFill([self bounds]);
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:color forKey:@"color"];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if(self = [super initWithCoder:aDecoder]) {
        color = [aDecoder decodeObjectForKey:@"color"];
    }
    return self;
}
@end

In the above code, I first set the color to be filled as green and just after the drawRect: method finishes change it to yellow, so the color that gets saved is yellow. But nevertheless, it reverts to null in the initWithFrame: log comment and the I get a green screen again. From the looks of it, the only way seems to be separating data from the view. But if there's a simpler way, can anyone help me ?

Lance
  • 8,872
  • 2
  • 36
  • 47
  • How is it being saved? When are you encoding it? And then, if you encode it, you need to recreate using the initWithCoder: method, not initWithFrame: The code above, though messy, should work if used properly. – Wain Apr 13 '13 at 08:02
  • I am using a document based application. And this is encoded when that document is saved. There is an outlet to it on the main file that is archived using NSKeyedArchiver. – Shan Mohsin Apr 13 '13 at 08:35
  • 1
    So, the correct way to structure your app is to have the colour attribute in your document, save it there with the other contents of the document and then pass it to the view. The document controller should do the passing. This is the basis of MVC structure - Model stores data, View displays it, Controller mediates between the 2. – Wain Apr 13 '13 at 08:54
  • 1
    To add why your current code doesn't work, the outlet is filled from the NIB file at runtime, not from whatever archiving you did as part of your document. I'd need to see that code to give more detail but you should learn to structure the app properly rather than experiment with something you'll never actually do. – Wain Apr 13 '13 at 08:55

1 Answers1

-3

This portion of your code doesn't make sense...

@implementation Tragic {
NSColor *color;

}

You do not declare instance variables in the implementation (".m") file. You do that in the interface (".h") file. The .m file should not have those brackets around the implementation statement either.

You can create the color variable in the .m file, but it will not be an "instance" variable accessible from other classes and if you do it it shouldn't be within any brackets.

regulus6633
  • 18,848
  • 5
  • 41
  • 49