-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDDataStorage.m
98 lines (78 loc) · 1.75 KB
/
PDDataStorage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// DataStorage.m
// Bodyspanner
//
// Created by Peter Denniss on 15/09/11.
// Copyright 2011 Go1 Pty Ltd. All rights reserved.
//
#import "PDDataStorage.h"
#import "PDUtilities.h"
#define kFilename @"PDDataStorage"
// ref: http://www.duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/
static PDDataStorage* sharedInstance = nil;
@implementation PDDataStorage
- (id)init
{
self = [super init];
if (self)
{
// Initialization code here.
storageDict = [NSKeyedUnarchiver unarchiveObjectWithFile:[PDUtilities getFilePathForFileInDocumentsDirectory:kFilename]];
if (storageDict == nil) {
storageDict = [NSMutableDictionary new];
}
}
return self;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if (sharedInstance == nil)
{
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
+ (PDDataStorage*) sharedStorage
{
@synchronized (self)
{
if (sharedInstance == nil)
{
[self new];
}
}
return sharedInstance;
}
- (void) setObject:(id)object forKey:(NSString*)key
{
if (object)
{
[storageDict setObject:object forKey:key];
}
}
- (id) objectForKey:(NSString*)key
{
return [storageDict objectForKey:key];
}
- (void) emptyStorage
{
[storageDict removeAllObjects];
}
- (void) removeObjectForKey:(NSString *)key
{
[storageDict removeObjectForKey:key];
}
- (void) save
{
NSString* path = [PDUtilities getFilePathForFileInDocumentsDirectory:kFilename];
[NSKeyedArchiver archiveRootObject:storageDict toFile:path];
}
@end