Skip to content

Instantly share code, notes, and snippets.

@cvasilak
Created January 10, 2013 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvasilak/4500441 to your computer and use it in GitHub Desktop.
Save cvasilak/4500441 to your computer and use it in GitHub Desktop.
#import "AGPropertyListStorage.h"
@implementation AGPropertyListStorage {
NSString* _file;
}
@synthesize type = _type;
+(id) storeWithConfig:(id<AGStoreConfig>) storeConfig {
return [[self alloc] initWithConfig:storeConfig];
}
-(id) initWithConfig:(id<AGStoreConfig>) storeConfig {
self = [super init];
if (self) {
// base inits:
_type = @"PLIST";
AGStoreConfiguration *config = (AGStoreConfiguration*) storeConfig;
_recordId = config.recordId;
// TODO: error handling
if (config.name == nil)
config.name = @"unnamed";
// calculate path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
_file = [documentsDirectory stringByAppendingPathComponent:config.name];
// if store exists initialize array
if ([[NSFileManager defaultManager] fileExistsAtPath:_file]) {
_array = [[NSMutableArray alloc] initWithContentsOfFile:_file];
} else {
_array = [[NSMutableArray alloc] init];
}
}
return self;
}
-(void) persist {
[_array writeToFile:_file atomically:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment