Decode and then encode an unknown class using NSKeyedArchiver?

I have a project file from an OS X application, which is a plist created using NSKeyedArchiver. I need to programmatically change one line.

Basically, it contains an NSDictionary object with Foundation classes. But there is one custom class (GradientColor). I defined it myself and did not try to do anything in initWithCoder: and encodeWithCoder: but the target application crashes trying to read the newly created project file. Therefore, during initialization, it cannot properly handle nil values.

Can I somehow find out which keys correspond to my class when initializing it with initWithCoder: (NSCoder *) aDecoder to encode them back unchanged?

+4
source share
1 answer

I restored the implementation of this class (GradientColor). In fact, 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 it serializes and deserializes correctly as the original implementation. I searched for the necessary keys and their types in the plist itself. Now my CLI utility generates valid project files.

Here I found a good post on the 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 /

+1
source

Source: https://habr.com/ru/post/1498469/


All Articles