Skip to content

Instantly share code, notes, and snippets.

@avielg
Created April 1, 2015 08:43
Show Gist options
  • Save avielg/032a7e48156e1dae19b4 to your computer and use it in GitHub Desktop.
Save avielg/032a7e48156e1dae19b4 to your computer and use it in GitHub Desktop.
Helper class for managing an extremely light key-value based db in one or more plist files
//
// GBPlistDB.h
// SensiyaSDK
//
// Created by Aviel Gross on 3/30/15.
// Copyright (c) 2015 Globalbit. All rights reserved.
//
// Mostly a wrapper class for the code found here: http://stackoverflow.com/a/6697423/2242359
#import <Foundation/Foundation.h>
@interface GBPlistDB : NSObject
/**
* Will create a new plist if not exist
*/
- (NSMutableDictionary *)plistDictionaryForName:(NSString *)name;
/**
* If key does note exist, will create the new key-value pair, else updates to value saved under key.
* Will create a new plist with the name, if one does not already exists
*
* @param value The value to enter, must be a Property List object
* @param key The key for the value
* @param name The name of the plist file to update
*/
- (void)setValue:(id <NSCoding, NSCopying>)value forKey:(NSString *)key inPlistNamed:(NSString *)name;
/**
* @param key The key for the value
* @param name The name of the plist to search for the key
* @return The value for the key, or nil
*/
- (id <NSCoding, NSCopying>)valueForKey:(NSString *)key inPlistNamed:(NSString *)name;
@end
//
// GBPlistDB.m
// SensiyaSDK
//
// Created by Aviel Gross on 3/30/15.
// Copyright (c) 2015 Globalbit. All rights reserved.
//
#import "GBPlistDB.h"
@implementation GBPlistDB
- (NSString *)plistPathForName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths.firstObject;
NSString *component = [NSString stringWithFormat:@"%@.plist", name];
NSString *path = [documentsDirectory stringByAppendingPathComponent:component];
return path;
}
- (NSMutableDictionary *)plistDictionaryForPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: path]) {
data = [NSMutableDictionary dictionaryWithContentsOfFile:path];
} else {
data = [NSMutableDictionary dictionary];
}
return data;
}
- (BOOL)plistExistAtPath:(NSString *)path {
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
- (NSMutableDictionary *)plistDictionaryForName:(NSString *)name {
NSString *path = [self plistPathForName:name];
return [self plistDictionaryForPath:path];
}
- (void)setValue:(id <NSCoding, NSCopying>)value forKey:(NSString *)key inPlistNamed:(NSString *)name {
NSString *path = [self plistPathForName:name];
NSMutableDictionary *dict = [self plistDictionaryForPath:name];
[dict setObject:value forKey:key];
[dict writeToFile:path atomically:YES];
}
- (id <NSCoding, NSCopying>)valueForKey:(NSString *)key inPlistNamed:(NSString *)name {
NSString *path = [self plistPathForName:name];
if (![self plistExistAtPath:path]) {
return nil;
}
NSMutableDictionary *plistDict = [self plistDictionaryForPath:path];
id value = [plistDict objectForKey:key];
return value;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment