Last active
June 9, 2017 16:27
-
-
Save derektu/4759996 to your computer and use it in GitHub Desktop.
Helper functions for doing object serialization (to plist file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ArchiveHelper.h | |
// | |
@interface ArchiveHelper : NSObject | |
+ (id)loadObjectFromFile:(NSString*)filePath withKey:(NSString*)key; | |
+ (void)saveObject:(id)object toFile:(NSString*)filePath withKey:(NSString*)key; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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