Pergunta

I've got a project file from OS X application that is a plist generated with NSKeyedArchiver. I need to programmatically change one string in it.

Basically, it contains NSDictionary object with Foundation classes. But there is one custom class (GradientColor). I've defined it myself and tried doing nothing in initWithCoder: and encodeWithCoder: but target application crashes trying to read newly generated project file. So it cannot handle nil values properly when initializing.

Can I somehow know which keys are corresponding to my class when initializing it with initWithCoder:(NSCoder *)aDecoder in order to encode them back unchanged?

Foi útil?

Solução

I've restored implementation of that class (GradientColor). Actually, it stores a really small amount of data:

@interface GradientColor : NSView <NSCoding> {
    float location;
    NSColor *color;
}
@end

@implementation GradientColor
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeFloat:location forKey:@"location"];
    [aCoder encodeObject:color forKey:@"color"];
}

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

My version does nothing, but serializes and deserializes correctly as original implementation. I've looked needed keys and their types in plist itself. Now my CLI utility generates valid project files.

Here I found a good post on internal structure of NSKeyedArchive, it helped me a lot: http://digitalinvestigation.wordpress.com/2012/04/04/geek-post-nskeyedarchiver-files-what-are-they-and-how-can-i-use-them/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top