Created
March 13, 2012 01:10
-
-
Save codeincontext/2025953 to your computer and use it in GitHub Desktop.
SKUserData singleton save-on-write dictionary subclass
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 characters
#import <Foundation/Foundation.h> | |
@interface SKUserData : NSMutableDictionary | |
+ (id)sharedInstance; | |
@end | |
#import "SKUserData.h" | |
@implementation SKUserData | |
+ (id)sharedInstance { | |
static dispatch_once_t pred = 0; | |
__strong static id _sharedObject = nil; | |
dispatch_once(&pred, ^{ | |
_sharedObject = [[self alloc] init]; | |
}); | |
return _sharedObject; | |
} | |
-(id)init { | |
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"userData" ofType:@"plist"]; | |
self = [[NSMutableDictionary dictionaryWithContentsOfFile:plistPath] retain]; | |
NSAssert(self, @"plist doesn't exist"); | |
return self; | |
} | |
- (void)setValue:(NSString*)value forKey:(NSString*)key { | |
[super setValue:value forKey:key]; | |
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"userData" ofType:@"plist"]; | |
[self writeToFile:plistPath atomically:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment