-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONModel+ArchiveProperty.m
47 lines (40 loc) · 1.09 KB
/
JSONModel+ArchiveProperty.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
//
// Created by Eli Greenfeld on 3/18/14.
//
#import <objc/runtime.h>
#import "JSONModel+ArchiveProperty.h"
@implementation JSONModel (ArchiveProperty)
- (NSArray *)keysForEncoding;
{
NSMutableArray *propList = [NSMutableArray array];
unsigned int numProps = 0;
unsigned int i = 0;
objc_property_t *props = class_copyPropertyList([self class], &numProps);
for (i = 0; i < numProps; i++) {
NSString *prop = [NSString stringWithUTF8String:property_getName(props[i])];
[propList addObject:prop];
}
return [propList copy];
}
// we are being created based on what was archived earlier
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
for (NSString *key in self.keysForEncoding)
{
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}
}
return self;
}
// we are asked to be archived, encode all our data
- (void)encodeWithCoder:(NSCoder *)aCoder
{
for (NSString *key in self.keysForEncoding)
{
[aCoder encodeObject:[self valueForKey:key] forKey:key];
}
}
@end