Skip to content

Instantly share code, notes, and snippets.

@derektu
Last active June 9, 2017 16:27
Show Gist options
  • Save derektu/4759996 to your computer and use it in GitHub Desktop.
Save derektu/4759996 to your computer and use it in GitHub Desktop.
Helper functions for doing object serialization (to plist file)
// ArchiveHelper.h
//
@interface ArchiveHelper : NSObject
+ (id)loadObjectFromFile:(NSString*)filePath withKey:(NSString*)key;
+ (void)saveObject:(id)object toFile:(NSString*)filePath withKey:(NSString*)key;
@end
// ArchiveHelper.m
//
#import "ArchiveHelper.h"
+ (id)loadObjectFromFile:(NSString*)filePath withKey:(NSString*)key
{
NSData* data = [[NSData alloc] initWithContentsOfFile:filePath];
NSKeyedUnarchiver* ar = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id object = [ar decodeObjectForKey:key];
[ar finishDecoding];
return object;
}
+ (void)saveObject:(id)object toFile:(NSString*)filePath withKey:(NSString*)key
{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *ar = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[ar encodeObject:object forKey:key];
[ar finishEncoding];
[data writeToFile:filePath atomically:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment