-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodinatorDocument.m
74 lines (40 loc) · 1.82 KB
/
CodinatorDocument.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
//
// CodinatorDocument.m
// Codinator
//
// Created by Vladimir Danila on 21/07/15.
// Copyright © 2015 Vladimir Danila. All rights reserved.
//
#import "CodinatorDocument.h"
#import "Polaris.h"
@implementation CodinatorDocument
- (void)encodeObject:(id<NSCoding>)object toWrappers:(NSMutableDictionary *)wrappers preferredFilename:(NSString *)preferredFilename {
@autoreleasepool {
NSMutableData * data = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:object forKey:@"data"];
[archiver finishEncoding];
NSFileWrapper * wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
[wrappers setObject:wrapper forKey:preferredFilename];
}
}
- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
NSMutableDictionary * wrappers = [NSMutableDictionary dictionary];
NSFileWrapper * fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];
return fileWrapper;
}
- (id)decodeObjectFromWrapperWithPreferredFilename:(NSString *)preferredFilename {
NSFileWrapper * fileWrapper = [self.fileWrapper.fileWrappers objectForKey:preferredFilename];
if (!fileWrapper) {
NSLog(@"Unexpected error: Couldn't find %@ in file wrapper!", preferredFilename);
return nil;
}
NSData * data = [fileWrapper regularFileContents];
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
return [unarchiver decodeObjectForKey:@"data"];
}
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
self.fileWrapper = (NSFileWrapper *) contents;
return YES;
}
@end