forked from greenisus/NSObject-NSCoding
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Archiver.m
51 lines (41 loc) · 1.96 KB
/
Archiver.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
//
// Archiver.m
// OpenStack
//
// Created by Mike Mayo on 10/4/10.
// The OpenStack project is provided under the Apache 2.0 license.
//
#import "Archiver.h"
@implementation Archiver
+ (id)retrieve:(NSString *)key {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
+ (BOOL)persist:(id)object key:(NSString *)key {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
return [NSKeyedArchiver archiveRootObject:object toFile:filePath];
}
+ (BOOL)delete:(NSString *)key {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
return [fileManager removeItemAtPath:filePath error:NULL];
}
+ (BOOL)deleteEverything {
BOOL result = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
for (int i = 0; i < [files count]; i++) {
NSString *path = [files objectAtIndex:i];
result = result && [fileManager removeItemAtPath:path error:NULL];
}
return result;
}
@end