Skip to content

Instantly share code, notes, and snippets.

@valexa
Created September 25, 2010 16:44
Show Gist options
  • Save valexa/597040 to your computer and use it in GitHub Desktop.
Save valexa/597040 to your computer and use it in GitHub Desktop.
#import "VAUserDefaults.h"
@implementation VAUserDefaults
- (id)initWithPlist:(NSString *)plist{
if (self = [super init]) {
//listen for notifications
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(theEvent:) name:@"VAUserDefaultsTo" object:nil];
path = [[NSString stringWithFormat:@"%@/Library/Preferences/%@",NSHomeDirectory(),plist] retain];
dict = [[NSMutableDictionary dictionaryWithContentsOfFile:path] retain];
NSLog(@"VAUserDefaults loaded %@",path);
}
return self;
}
- (void)dealloc {
[path release];
[dict release];
[super dealloc];
}
- (NSDictionary*)dictionaryRepresentation{
return dict;
}
-(void)theEvent:(NSNotification*)notif{
if ([[notif name] isEqualToString:@"VAUserDefaultsTo"]) {
NSDictionary *indict = [NSDictionary dictionaryWithContentsOfFile:path];
[dict addEntriesFromDictionary:indict];
//NSLog(@"VAUserDefaultsTo");
}
}
- (void)notification{
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"VAUserDefaultsFrom" object:nil userInfo:nil];
//NSLog(@"VAUserDefaultsFrom");
}
- (BOOL)synchronize{
//NSLog(@"sync , changes will be lost if you read something between setting and synchronizing");
NSMutableDictionary *outdict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[outdict addEntriesFromDictionary:dict];
[outdict writeToFile:path atomically:YES];
[self notification];
return TRUE;
}
- (id)persistentDomainForName:(NSString *)arg1{
NSString *p =[NSString stringWithFormat:@"%@/Library/Preferences/%@.plist",NSHomeDirectory(),arg1];
return [NSDictionary dictionaryWithContentsOfFile:p];
//NSLog(@"VAUserDefaults read %@",p);
}
- (void)setPersistentDomain:(NSDictionary *)arg1 forName:(NSString *)arg2{
NSString *p =[NSString stringWithFormat:@"%@/Library/Preferences/%@.plist",NSHomeDirectory(),arg2];
[arg1 writeToFile:p atomically:YES];
//NSLog(@"VAUserDefaults saved %i values in %@",[arg1 count],p);
[self notification];
}
- (id)objectForKey:(NSString *)arg1{
NSDictionary *indict = [NSDictionary dictionaryWithContentsOfFile:path];
[dict addEntriesFromDictionary:indict];
return [dict objectForKey:arg1];
}
- (BOOL)boolForKey:(NSString *)arg1{
NSDictionary *indict = [NSDictionary dictionaryWithContentsOfFile:path];
[dict addEntriesFromDictionary:indict];
return [[dict objectForKey:arg1] boolValue];
}
- (long long)integerForKey:(NSString *)arg1{
NSDictionary *indict = [NSDictionary dictionaryWithContentsOfFile:path];
[dict addEntriesFromDictionary:indict];
return [[dict objectForKey:arg1] intValue];
}
- (void)setObject:(id)arg1 forKey:(id)arg2{
[dict setObject:arg1 forKey:arg2];
}
- (void)setBool:(BOOL)arg1 forKey:(id)arg2{
[dict setObject:[NSNumber numberWithBool:arg1] forKey:arg2];
}
- (void)setInteger:(long long)arg1 forKey:(id)arg2{
[dict setObject:[NSNumber numberWithInt:arg1] forKey:arg2];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment