Created
April 1, 2015 08:43
-
-
Save avielg/032a7e48156e1dae19b4 to your computer and use it in GitHub Desktop.
Revisions
-
avielg created this gist
Apr 1, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ // // 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 This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ // // 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