Skip to content

Instantly share code, notes, and snippets.

@squm
Created January 31, 2014 10:41
Show Gist options
  • Save squm/8729857 to your computer and use it in GitHub Desktop.
Save squm/8729857 to your computer and use it in GitHub Desktop.
secure coding
typedef NS_ENUM(int32_t, CFGGame) {
CFGGameWinter,
CFGGameSummer,
CFGGameVivaldi
};
@interface Person : NSObject <NSSecureCoding>
@property int32_t cfgVersion;
@property (retain, nonatomic) NSDictionary *cfgReserved;
@property CFGGame cfgGame;
@property (retain, nonatomic) NSDate *cfgDate;
@property (retain, nonatomic) NSURL *cfgMovie;
@property (retain, nonatomic) NSArray *cfgImages;
@end
@implementation Person
/*
* TODO: handle multiple versions of each. That is, explicit convert to
* and from integer. Manage insertions of additional enum values
*
*/
static const int32_t kArchiveVersion = 123;
- (instancetype)
initWithCoder:(NSCoder *)coder {
self = [super init];
if (!self) {
return self;
}
self.cfgVersion = [coder decodeInt32ForKey:@"cfgVersion"];
if (self.cfgVersion != kArchiveVersion) {
NSLog(@"Person init: unknown version");
}
self.cfgVersion = kArchiveVersion;
self.cfgReserved = [coder decodeObjectOfClass:[NSDictionary class] forKey:@"cfgReserved"];
self.cfgGame = [coder decodeInt32ForKey:@"cfgGame"];
self.cfgName = [coder decodeObjectOfClass:[NSString class] forKey:@"cfgName"];
self.cfgDate = [coder decodeObjectOfClass:[NSDate class] forKey:@"cfgDate"];
self.cfgMovie = [coder decodeObjectOfClass:[NSURL class] forKey:@"cfgMovie"];
self.cfgImages = [coder decodeObjectOfClass:[NSArray class] forKey:@"cfgImages"];
return self;
}
- (void)
encodeWithCoder:(NSCoder *)coder {
if ([coder isKindOfClass:[NSKeyedArchiver class]]) {
}
else {
[NSException raise:NSInvalidArchiveOperationException
format:@"Person only supports NSKeyedArchiver coders"];
}
self.cfgVersion = kArchiveVersion;
[coder encodeInt32: self.cfgVersion forKey:@"cfgVersion"];
[coder encodeObject:self.cfgReserved forKey:@"cfgReserved"];
[coder encodeInt32:self.cfgGame forKey:@"cfgGame"];
[coder encodeObject:self.cfgName forKey:@"cfgName"];
[coder encodeObject:self.cfgDate forKey:@"cfgDate"];
[coder encodeObject:self.cfgMovie forKey:@"cfgMovie"];
[coder encodeObject:self.cfgImages forKey:@"cfgImages"];
}
+ (BOOL)
supportsSecureCoding {
return YES;
}
- (NSString *)
description {
return @"otiose otiose otiose otiose otiose";
}
@end
@implementation State
- (instancetype)
initWithCoder:(NSCoder *)coder {
}
- (void)
encodeWithCoder:(NSCoder *)coder {
}
+ (BOOL)
supportsSecureCoding {
return YES;
}
- (void)
stateTest {
self.statePerson = @[
[Person new],
[Person new],
[Person new],
[Person new],
[Person new]
];
}
- (void)
stateSave {
// Archiving calls encodeWithCoder
NSURL *path = [State stateArchivePath];
BOOL isArchived = [NSKeyedArchiver archiveRootObject:self toFile:path.path];
NSAssert(isArchived, @"State error: archiveRootObject");
}
+ (State *)
stateLoad {
NSURL *path = [State stateArchivePath];
State *state = [NSKeyedUnarchiver unarchiveObjectWithFile:path.path];
if (!state) {
NSLog(@"State stateLoad: new");
state = [State new];
state.statePerson = [NSMutableArray new];
}
return state;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment