-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathNSObject+NSCoding.m
283 lines (259 loc) · 11.6 KB
/
NSObject+NSCoding.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// NSObject+NSCoding.m
// OpenStack
//
// Created by Michael Mayo on 3/4/11.
// The OpenStack project is provided under the Apache 2.0 license.
//
#import "NSObject+NSCoding.h"
#import <objc/runtime.h>
@implementation NSObject (NSCoding)
- (NSMutableDictionary *)propertiesForClass:(Class)klass {
NSMutableDictionary *results = [[[NSMutableDictionary alloc] init] autorelease];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
for(i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *pname = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
NSString *pattrs = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
pattrs = [[pattrs componentsSeparatedByString:@","] objectAtIndex:0];
pattrs = [pattrs substringFromIndex:1];
[results setObject:pattrs forKey:pname];
}
free(properties);
if ([klass superclass] != [NSObject class]) {
[results addEntriesFromDictionary:[self propertiesForClass:[klass superclass]]];
}
return results;
}
- (NSDictionary *)properties {
return [self propertiesForClass:[self class]];
}
- (void)autoEncodeWithCoder:(NSCoder *)coder {
NSDictionary *properties = [self properties];
for (NSString *key in properties) {
NSString *type = [properties objectForKey:key];
id value;
unsigned long long ullValue;
long long llValue;
BOOL boolValue;
float floatValue;
double doubleValue;
NSInteger intValue;
unsigned long ulValue;
long longValue;
unsigned unsignedValue;
short shortValue;
NSString *className;
NSMethodSignature *signature = [self methodSignatureForSelector:NSSelectorFromString(key)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:NSSelectorFromString(key)];
[invocation setTarget:self];
switch ([type characterAtIndex:0]) {
case '@': // object
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
value = [self performSelector:NSSelectorFromString(key)];
// only decode if the property conforms to NSCoding
if([class conformsToProtocol:@protocol(NSCoding)]){
[coder encodeObject:value forKey:key];
}
}
break;
case 'B':
[invocation invoke];
[invocation getReturnValue:&boolValue];
[coder encodeObject:[NSNumber numberWithBool:boolValue] forKey:key];
break;
case 'c': // bool
[invocation invoke];
[invocation getReturnValue:&boolValue];
[coder encodeObject:[NSNumber numberWithBool:boolValue] forKey:key];
break;
case 'f': // float
[invocation invoke];
[invocation getReturnValue:&floatValue];
[coder encodeObject:[NSNumber numberWithFloat:floatValue] forKey:key];
break;
case 'd': // double
[invocation invoke];
[invocation getReturnValue:&doubleValue];
[coder encodeObject:[NSNumber numberWithDouble:doubleValue] forKey:key];
break;
case 'i': // int
[invocation invoke];
[invocation getReturnValue:&intValue];
[coder encodeObject:[NSNumber numberWithInteger:intValue] forKey:key];
break;
case 'L': // unsigned long
[invocation invoke];
[invocation getReturnValue:&ulValue];
[coder encodeObject:[NSNumber numberWithUnsignedLong:ulValue] forKey:key];
break;
case 'Q': // unsigned long long
[invocation invoke];
[invocation getReturnValue:&ullValue];
[coder encodeObject:[NSNumber numberWithUnsignedLongLong:ullValue] forKey:key];
break;
case 'q': // long long
[invocation invoke];
[invocation getReturnValue:&llValue];
[coder encodeObject:[NSNumber numberWithLongLong:llValue] forKey:key];
break;
case 'l': // long
[invocation invoke];
[invocation getReturnValue:&longValue];
[coder encodeObject:[NSNumber numberWithLong:longValue] forKey:key];
break;
case 's': // short
[invocation invoke];
[invocation getReturnValue:&shortValue];
[coder encodeObject:[NSNumber numberWithShort:shortValue] forKey:key];
break;
case 'I': // unsigned
[invocation invoke];
[invocation getReturnValue:&unsignedValue];
[coder encodeObject:[NSNumber numberWithUnsignedInt:unsignedValue] forKey:key];
break;
default:
break;
}
}
}
- (void)autoDecode:(NSCoder *)coder {
NSDictionary *properties = [self properties];
for (NSString *key in properties) {
NSString *ivarKey = [@"_"stringByAppendingString:key];
NSString *type = [properties objectForKey:key];
NSNumber *number;
unsigned int addr;
NSInteger i;
CGFloat f;
BOOL b;
double d;
unsigned long ul;
unsigned long long ull;
long longValue;
long long longLongValue;
unsigned unsignedValue;
short shortValue;
Ivar ivar;
bool *varIndexBool;
float *varIndexFloat;
double *varIndexDouble;
unsigned long long *varIndexULongLong;
unsigned long *varIndexULong;
long *varIndexLong;
long long *varIndexLongLong;
unsigned *varU;
short *varShort;
NSString *className;
switch ([type characterAtIndex:0]) {
case '@': // object
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
// only decode if the property conforms to NSCoding
if ([class conformsToProtocol:@protocol(NSCoding )]){
id value = [coder decodeObjectForKey:key];
// object_setInstanceVariable(self, [ivarKey UTF8String], &value);
[self setValue:value forKey:key];
}
}
break;
case 'B': // bool
number = [coder decodeObjectForKey:key];
b = [number boolValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexBool = (bool *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexBool = b;
}
break;
case 'c': // bool
number = [coder decodeObjectForKey:key];
b = [number boolValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexBool = (bool *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexBool = b;
}
break;
case 'f': // float
number = [coder decodeObjectForKey:key];
f = [number floatValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexFloat = (float *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexFloat = f;
}
break;
case 'd': // double
number = [coder decodeObjectForKey:key];
d = [number doubleValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexDouble = (double *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexDouble = d;
}
break;
case 'i': // int
number = [coder decodeObjectForKey:key];
i = [number intValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexLong = (long *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexLong = i;
}
break;
case 'L': // unsigned long
number = [coder decodeObjectForKey:key];
ul = [number unsignedLongValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexULong = (unsigned long *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexULong = ul;
}
break;
case 'Q': // unsigned long long
number = [coder decodeObjectForKey:key];
ull = [number unsignedLongLongValue];
addr = (unsigned int)&ull;
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexULongLong = (unsigned long long *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexULongLong = ull;
}
break;
case 'l': // long
number = [coder decodeObjectForKey:key];
longValue = [number longValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexLong = (long *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexLong = longValue;
}
break;
case 'I': // unsigned
number = [coder decodeObjectForKey:key];
unsignedValue = [number unsignedIntValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varU = (unsigned *)(void **)((char *)self + ivar_getOffset(ivar));
*varU = unsignedValue;
}
break;
case 's': // short
number = [coder decodeObjectForKey:key];
shortValue = [number shortValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varShort = (short *)(void **)((char *)self + ivar_getOffset(ivar));
*varShort = shortValue;
}
break;
case 'q': // long long
number = [coder decodeObjectForKey:key];
longLongValue = [number longLongValue];
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
varIndexLongLong = (long long *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndexLongLong = longLongValue;
}
break;
default:
break;
}
}
}
@end