Skip to content

Commit 1f2a849

Browse files
author
Josh Holtz
committed
Added NSCoding to model
1 parent 7c00eff commit 1f2a849

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

Classes/JSONAPIResource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
@interface JSONAPIResource : NSObject
11+
@interface JSONAPIResource : NSObject<NSCoding>
1212

1313
@property (nonatomic, strong) id ID;
1414
@property (nonatomic, strong) NSString *href;

Classes/JSONAPIResource.m

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#import "JSONAPIResourceLinker.h"
1212

13+
#import <objc/runtime.h>
14+
#import <objc/message.h>
15+
1316
#pragma mark - JSONAPIResource
1417

1518
@interface JSONAPIResource()
@@ -176,4 +179,77 @@ - (void)linkLinks:(NSDictionary*)linked {
176179
}
177180
}
178181

182+
#pragma mark - NSCoding
183+
184+
- (NSArray *)propertyKeys
185+
{
186+
NSMutableArray *array = [NSMutableArray array];
187+
Class class = [self class];
188+
while (class != [NSObject class])
189+
{
190+
unsigned int propertyCount;
191+
objc_property_t *properties = class_copyPropertyList(class, &propertyCount);
192+
for (int i = 0; i < propertyCount; i++)
193+
{
194+
//get property
195+
objc_property_t property = properties[i];
196+
const char *propertyName = property_getName(property);
197+
NSString *key = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
198+
199+
//check if read-only
200+
BOOL readonly = NO;
201+
const char *attributes = property_getAttributes(property);
202+
NSString *encoding = [NSString stringWithCString:attributes encoding:NSUTF8StringEncoding];
203+
if ([[encoding componentsSeparatedByString:@","] containsObject:@"R"])
204+
{
205+
readonly = YES;
206+
207+
//see if there is a backing ivar with a KVC-compliant name
208+
NSRange iVarRange = [encoding rangeOfString:@",V"];
209+
if (iVarRange.location != NSNotFound)
210+
{
211+
NSString *iVarName = [encoding substringFromIndex:iVarRange.location + 2];
212+
if ([iVarName isEqualToString:key] ||
213+
[iVarName isEqualToString:[@"_" stringByAppendingString:key]])
214+
{
215+
//setValue:forKey: will still work
216+
readonly = NO;
217+
}
218+
}
219+
}
220+
221+
if (!readonly)
222+
{
223+
//exclude read-only properties
224+
[array addObject:key];
225+
}
226+
}
227+
free(properties);
228+
class = [class superclass];
229+
}
230+
return array;
231+
}
232+
233+
- (id)initWithCoder:(NSCoder *)aDecoder
234+
{
235+
if ((self = [self init]))
236+
{
237+
for (NSString *key in [self propertyKeys])
238+
{
239+
id value = [aDecoder decodeObjectForKey:key];
240+
[self setValue:value forKey:key];
241+
}
242+
}
243+
return self;
244+
}
245+
246+
- (void)encodeWithCoder:(NSCoder *)aCoder
247+
{
248+
for (NSString *key in [self propertyKeys])
249+
{
250+
id value = [self valueForKey:key];
251+
[aCoder encodeObject:value forKey:key];
252+
}
253+
}
254+
179255
@end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
6+
<false/>
7+
<key>IDESourceControlProjectIdentifier</key>
8+
<string>DA49755E-FD70-48BA-8E28-2C90B0ABED83</string>
9+
<key>IDESourceControlProjectName</key>
10+
<string>JSONAPI</string>
11+
<key>IDESourceControlProjectOriginsDictionary</key>
12+
<dict>
13+
<key>7EE6692C-FAB2-484E-B1C6-2C33369BAE38</key>
14+
<string>ssh://github.com/joshdholtz/jsonapi-ios.git</string>
15+
</dict>
16+
<key>IDESourceControlProjectPath</key>
17+
<string>Project/JSONAPI.xcworkspace</string>
18+
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
19+
<dict>
20+
<key>7EE6692C-FAB2-484E-B1C6-2C33369BAE38</key>
21+
<string>../..</string>
22+
</dict>
23+
<key>IDESourceControlProjectURL</key>
24+
<string>ssh://github.com/joshdholtz/jsonapi-ios.git</string>
25+
<key>IDESourceControlProjectVersion</key>
26+
<integer>110</integer>
27+
<key>IDESourceControlProjectWCCIdentifier</key>
28+
<string>7EE6692C-FAB2-484E-B1C6-2C33369BAE38</string>
29+
<key>IDESourceControlProjectWCConfigurations</key>
30+
<array>
31+
<dict>
32+
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
33+
<string>public.vcs.git</string>
34+
<key>IDESourceControlWCCIdentifierKey</key>
35+
<string>7EE6692C-FAB2-484E-B1C6-2C33369BAE38</string>
36+
<key>IDESourceControlWCCName</key>
37+
<string>JSONAPI</string>
38+
</dict>
39+
</array>
40+
</dict>
41+
</plist>

0 commit comments

Comments
 (0)