Skip to content

Commit 01c79bb

Browse files
committed
Merge branch 'master' of github.com:kostassite/NSObject-NSCoding
Conflicts: NSObject+NSCoding.m
2 parents 2d947c4 + 3dea58c commit 01c79bb

File tree

3 files changed

+129
-17
lines changed

3 files changed

+129
-17
lines changed

Archiver.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ + (id)retrieve:(NSString *)key {
1414
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
1515
NSString *documentsDirectory = [paths objectAtIndex:0];
1616
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
17-
return [[[NSKeyedUnarchiver unarchiveObjectWithFile:filePath] retain] autorelease];
17+
return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
1818
}
1919

2020
+ (BOOL)persist:(id)object key:(NSString *)key {

NSObject+NSCoding.h

100644100755
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@
1616
- (NSDictionary *)properties;
1717

1818
@end
19+
20+
#define AUTO_ENCODE - (void)encodeWithCoder:(NSCoder *)coder { \
21+
[self autoEncodeWithCoder:coder]; \
22+
}
23+
24+
25+
#define AUTO_DECODE - (id)initWithCoder:(NSCoder *)coder { \
26+
if ((self = [super init])) { \
27+
[self autoDecode:coder]; \
28+
} \
29+
return self; \
30+
}

NSObject+NSCoding.m

100644100755
Lines changed: 116 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@
99
#import "NSObject+NSCoding.h"
1010
#import <objc/runtime.h>
1111

12-
1312
@implementation NSObject (NSCoding)
1413

15-
- (NSMutableDictionary *)propertiesForClass:(Class)klass {
14+
- (NSMutableDictionary *) propertiesForClass:(Class)klass
15+
{
1616

17-
NSMutableDictionary *results = [[[NSMutableDictionary alloc] init] autorelease];
17+
NSMutableDictionary *results = [NSMutableDictionary new];
1818

1919
unsigned int outCount, i;
2020
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
2121
for(i = 0; i < outCount; i++) {
2222
objc_property_t property = properties[i];
23-
23+
2424
NSString *pname = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
2525
NSString *pattrs = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
2626

27-
pattrs = [[pattrs componentsSeparatedByString:@","] objectAtIndex:0];
27+
NSArray *comps = [pattrs componentsSeparatedByString:@","];
28+
pattrs = [comps objectAtIndex:0];
2829
pattrs = [pattrs substringFromIndex:1];
29-
30+
3031
[results setObject:pattrs forKey:pname];
3132
}
3233
free(properties);
@@ -38,13 +39,17 @@ - (NSMutableDictionary *)propertiesForClass:(Class)klass {
3839
return results;
3940
}
4041

41-
- (NSDictionary *)properties {
42+
- (NSDictionary *) properties
43+
{
4244
return [self propertiesForClass:[self class]];
4345
}
4446

45-
- (void)autoEncodeWithCoder:(NSCoder *)coder {
47+
- (void) autoEncodeWithCoder:(NSCoder *)coder
48+
{
4649
NSDictionary *properties = [self properties];
47-
for (NSString *key in properties) {
50+
51+
for (NSString *key in properties)
52+
{
4853
NSString *type = [properties objectForKey:key];
4954
id value;
5055
unsigned long long ullValue;
@@ -62,14 +67,15 @@ - (void)autoEncodeWithCoder:(NSCoder *)coder {
6267
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
6368
[invocation setSelector:NSSelectorFromString(key)];
6469
[invocation setTarget:self];
65-
6670
switch ([type characterAtIndex:0]) {
6771
case '@': // object
6872
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
6973
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
7074
Class class = NSClassFromString(className);
75+
#pragma clang diagnostic push
76+
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
7177
value = [self performSelector:NSSelectorFromString(key)];
72-
78+
#pragma clang diagnostic pop
7379
// only decode if the property conforms to NSCoding
7480
if([class conformsToProtocol:@protocol(NSCoding)]){
7581
[coder encodeObject:value forKey:key];
@@ -137,23 +143,39 @@ - (void)autoEncodeWithCoder:(NSCoder *)coder {
137143
}
138144
}
139145

140-
- (void)autoDecode:(NSCoder *)coder {
146+
- (void) autoDecode:(NSCoder *)coder
147+
{
141148
NSDictionary *properties = [self properties];
149+
<<<<<<< HEAD
142150
for (NSString *key in properties) {
143151
NSString *ivarKey = [@"_"stringByAppendingString:key];
152+
=======
153+
154+
for (NSString *key in properties)
155+
{
156+
NSString *capitalizedKey = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] capitalizedString]];
157+
NSString *selectorString = [NSString stringWithFormat:@"set%@:", capitalizedKey];
158+
SEL selector = NSSelectorFromString(selectorString);
159+
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
160+
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
161+
[invocation setSelector:selector];
162+
[invocation setTarget:self];
163+
164+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
144165
NSString *type = [properties objectForKey:key];
145166
NSNumber *number;
146-
unsigned int addr;
147167
NSInteger i;
148168
CGFloat f;
149169
BOOL b;
150170
double d;
151171
unsigned long ul;
152172
unsigned long long ull;
173+
long long ll;
153174
long longValue;
154175
long long longLongValue;
155176
unsigned unsignedValue;
156177
short shortValue;
178+
<<<<<<< HEAD
157179
Ivar ivar;
158180

159181
bool *varIndexBool;
@@ -189,77 +211,151 @@ - (void)autoDecode:(NSCoder *)coder {
189211
*varIndexBool = b;
190212
}
191213
break;
214+
=======
215+
216+
switch ([type characterAtIndex:0])
217+
{
218+
case '@': // object
219+
if ([[type componentsSeparatedByString:@"\""] count] > 1)
220+
{
221+
NSString *className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
222+
Class class = NSClassFromString(className);
223+
// only decode if the property conforms to NSCoding
224+
if ([class conformsToProtocol:@protocol(NSCoding)]){
225+
@try {
226+
value = [coder decodeObjectForKey:key];
227+
}
228+
@catch (NSException *exception) {
229+
NSLog(@"Warning: %@", exception);
230+
continue;
231+
}
232+
#pragma clang diagnostic push
233+
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
234+
[self performSelector:selector withObject:value];
235+
#pragma clang diagnostic pop
236+
}
237+
}
238+
break;
239+
case 'B': // bool for 64bit
240+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
192241
case 'c': // bool
193-
number = [coder decodeObjectForKey:key];
242+
number = [coder decodeObjectForKey:key];
194243
b = [number boolValue];
244+
<<<<<<< HEAD
195245
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
196246
varIndexBool = (bool *)(void **)((char *)self + ivar_getOffset(ivar));
197247
*varIndexBool = b;
198248
}
249+
=======
250+
[invocation setArgument:&b atIndex:2];
251+
[invocation invoke];
252+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
199253
break;
200254
case 'f': // float
201-
number = [coder decodeObjectForKey:key];
255+
number = [coder decodeObjectForKey:key];
202256
f = [number floatValue];
257+
<<<<<<< HEAD
203258
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
204259
varIndexFloat = (float *)(void **)((char *)self + ivar_getOffset(ivar));
205260
*varIndexFloat = f;
206261
}
262+
=======
263+
[invocation setArgument:&f atIndex:2];
264+
[invocation invoke];
265+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
207266
break;
208-
case 'd': // double
267+
case 'd': // double
209268
number = [coder decodeObjectForKey:key];
210269
d = [number doubleValue];
270+
<<<<<<< HEAD
211271
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
212272
varIndexDouble = (double *)(void **)((char *)self + ivar_getOffset(ivar));
213273
*varIndexDouble = d;
214274
}
275+
=======
276+
[invocation setArgument:&d atIndex:2];
277+
[invocation invoke];
278+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
215279
break;
216280
case 'i': // int
217281
number = [coder decodeObjectForKey:key];
218282
i = [number intValue];
283+
<<<<<<< HEAD
219284
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
220285
varIndexLong = (long *)(void **)((char *)self + ivar_getOffset(ivar));
221286
*varIndexLong = i;
222287
}
288+
=======
289+
[invocation setArgument:&i atIndex:2];
290+
[invocation invoke];
291+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
223292
break;
224293
case 'L': // unsigned long
225294
number = [coder decodeObjectForKey:key];
226295
ul = [number unsignedLongValue];
296+
<<<<<<< HEAD
227297

228298
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
229299
varIndexULong = (unsigned long *)(void **)((char *)self + ivar_getOffset(ivar));
230300
*varIndexULong = ul;
231301
}
232302

303+
=======
304+
[invocation setArgument:&ul atIndex:2];
305+
[invocation invoke];
306+
break;
307+
case 'q': // long long
308+
number = [coder decodeObjectForKey:key];
309+
ll = [number longLongValue];
310+
[invocation setArgument:&ll atIndex:2];
311+
[invocation invoke];
312+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
233313
break;
234314
case 'Q': // unsigned long long
235315
number = [coder decodeObjectForKey:key];
236316
ull = [number unsignedLongLongValue];
317+
<<<<<<< HEAD
237318
addr = (unsigned int)&ull;
238319

239320
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
240321
varIndexULongLong = (unsigned long long *)(void **)((char *)self + ivar_getOffset(ivar));
241322
*varIndexULongLong = ull;
242323
}
324+
=======
325+
[invocation setArgument:&ull atIndex:2];
326+
[invocation invoke];
327+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
243328
break;
244329
case 'l': // long
245330
number = [coder decodeObjectForKey:key];
246331
longValue = [number longValue];
332+
<<<<<<< HEAD
247333
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
248334
varIndexLong = (long *)(void **)((char *)self + ivar_getOffset(ivar));
249335
*varIndexLong = longValue;
250336
}
337+
=======
338+
[invocation setArgument:&longValue atIndex:2];
339+
[invocation invoke];
340+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
251341
break;
252342
case 'I': // unsigned
253343
number = [coder decodeObjectForKey:key];
254344
unsignedValue = [number unsignedIntValue];
345+
<<<<<<< HEAD
255346
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
256347
varU = (unsigned *)(void **)((char *)self + ivar_getOffset(ivar));
257348
*varU = unsignedValue;
258349
}
350+
=======
351+
[invocation setArgument:&unsignedValue atIndex:2];
352+
[invocation invoke];
353+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
259354
break;
260355
case 's': // short
261356
number = [coder decodeObjectForKey:key];
262357
shortValue = [number shortValue];
358+
<<<<<<< HEAD
263359
if ((ivar = class_getInstanceVariable([self class], [ivarKey UTF8String]))) {
264360
varShort = (short *)(void **)((char *)self + ivar_getOffset(ivar));
265361
*varShort = shortValue;
@@ -272,6 +368,10 @@ - (void)autoDecode:(NSCoder *)coder {
272368
varIndexLongLong = (long long *)(void **)((char *)self + ivar_getOffset(ivar));
273369
*varIndexLongLong = longLongValue;
274370
}
371+
=======
372+
[invocation setArgument:&shortValue atIndex:2];
373+
[invocation invoke];
374+
>>>>>>> 3dea58c6b7c4fbc9b9d3ac6733fea86eeb73daff
275375
break;
276376

277377
default:

0 commit comments

Comments
 (0)