forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSDictionaryEntry.m
152 lines (122 loc) · 3.09 KB
/
SSDictionaryEntry.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#import "SSDictionaryEntry.h"
@implementation SSDictionaryEntry
- (id)key {
if (_key == nil) {
// We use an empty NSData to symbolize nil because
// NSNull is not serializable.
return [NSData data] ;
}
else {
return [[_key retain] autorelease] ;
}
}
- (id)value {
if (_value == nil) {
// We use an empty NSData to symbolize nil because
// NSNull is not serializable.
return [NSData data] ;
}
else {
return [[_value retain] autorelease] ;
}
}
- (NSMutableDictionary *)parent {
return [[_parent retain] autorelease];
}
- (void)setParent:(NSMutableDictionary *)value {
if (_parent != value) {
[_parent release];
_parent = [value retain];
}
}
- (void)updateParent {
// Update the parent dic, such as to trigger KVO
[[self parent] setObject:[self value] forKey:[self key]];
}
- (void)setKey:(id)key {
if (_key != key) {
[_key release];
_key = [key retain];
[self updateParent] ;
}
}
- (void)setValue:(id)value {
if (_value != value) {
[_value release];
_value = [value retain];
[self updateParent] ;
}
}
- (NSString*)description {
return [NSString stringWithFormat:@"SSDictionaryEntry with parent %@ <0x%x> and:\nkey <%@>:%@\nvalue <%@>: %@",
[[self parent] class],
[self parent],
[[self key] class],
[self key],
[[self value] class],
[self value]] ;
}
- (id)initWithParent:(NSMutableDictionary*)parent
key:(id)key
value:(id)value {
[self setParent:parent] ;
[self setKey:key] ;
[self setValue:value] ;
[self bind:@"value"
toObject:parent
withKeyPath:key
options:nil];
return self;
}
@end
@implementation DicToReadableValuesArray
+ (Class)transformedValueClass {
return [NSMutableArray class];
}
+ (BOOL)allowsReverseTransformation {
return YES ;
}
+ (id)readableNull {
return @"<NULL>" ;
}
- (id)transformedValue:(id)dic {
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:[dic count]] ;
NSEnumerator* e = [dic keyEnumerator] ;
NSString* key ;
while ((key = [e nextObject]) != nil) {
id value = [dic objectForKey:key] ;
// We used an empty NSData to symbolize nil because
// NSNull is not serializable.
if ([value isKindOfClass:[NSData class]]) {
if ([value length] == 0) {
value = [DicToReadableValuesArray readableNull] ;
}
}
SSDictionaryEntry* entry = [[SSDictionaryEntry alloc] initWithParent:dic
key:key
value:value] ;
[array addObject:entry] ;
[entry release] ;
}
return [array autorelease] ;
}
- (id)reverseTransformedValue:(id)array {
NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithCapacity:[array count]] ;
NSEnumerator* e = [array objectEnumerator] ;
SSDictionaryEntry* entry ;
while ((entry = [e nextObject]) != nil) {
id key = [entry valueForKey:@"key"] ;
if (key == nil) {
key = @"New Key" ;
}
id value = [entry valueForKey:@"value"] ;
if (value == nil) {
// We use an empty NSData to symbolize nil because
// NSNull is not serializable.
value = [NSData data] ;
}
[dic setObject:value forKey:key] ;
}
return [dic autorelease] ;
}
@end