Skip to content

Commit 4aa5f85

Browse files
committed
add nscoding for SHmodel
1 parent 5d5dd63 commit 4aa5f85

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

SHModelObject.xcodeproj/project.xcworkspace/xcshareddata/SHModelObject.xccheckout

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
66
<false/>
77
<key>IDESourceControlProjectIdentifier</key>
8-
<string>9DE4911E-4957-4F30-B892-61F2E6812059</string>
8+
<string>BBC6A4B8-4768-465C-B90C-4197CE7B15FD</string>
99
<key>IDESourceControlProjectName</key>
1010
<string>SHModelObject</string>
1111
<key>IDESourceControlProjectOriginsDictionary</key>
1212
<dict>
13-
<key>35F320BD-5827-4179-8696-C57E84417A71</key>
14-
<string>https://github.com/grevolution/SHModelObject.git</string>
13+
<key>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</key>
14+
<string>https://github.com/ndduy/SHModelObject.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
1717
<string>SHModelObject.xcodeproj/project.xcworkspace</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
20-
<key>35F320BD-5827-4179-8696-C57E84417A71</key>
20+
<key>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</key>
2121
<string>../..</string>
2222
</dict>
2323
<key>IDESourceControlProjectURL</key>
24-
<string>https://github.com/grevolution/SHModelObject.git</string>
24+
<string>https://github.com/ndduy/SHModelObject.git</string>
2525
<key>IDESourceControlProjectVersion</key>
2626
<integer>110</integer>
2727
<key>IDESourceControlProjectWCCIdentifier</key>
28-
<string>35F320BD-5827-4179-8696-C57E84417A71</string>
28+
<string>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</string>
2929
<key>IDESourceControlProjectWCConfigurations</key>
3030
<array>
3131
<dict>
3232
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
3333
<string>public.vcs.git</string>
3434
<key>IDESourceControlWCCIdentifierKey</key>
35-
<string>35F320BD-5827-4179-8696-C57E84417A71</string>
35+
<string>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</string>
3636
<key>IDESourceControlWCCName</key>
3737
<string>SHModelObject</string>
3838
</dict>

SHModelObject/SHAppDelegate.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5757
dateConversionOption:kDateConverstionFromNSStringToNSDateOption
5858
inputDateType:kInputDateFormatDotNetWithTimeZone mappings:mapping];
5959

60-
NSLog([modal description], nil);
60+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
61+
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
62+
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"appData"];
6163

64+
BOOL isArchived = [NSKeyedArchiver archiveRootObject:modal toFile:filePath];
65+
SHTestModal *unarchived = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
66+
67+
NSLog([modal description], nil);
68+
NSLog([unarchived description], nil);
69+
6270

6371
return YES;
6472
}

SHModelObject/SHModelObject/SHModelObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef NS_ENUM(int, kInputDateFormat) {
4646
* by passing a NSDictionary to it. The NSDictionary key is compared with the instance variable name and the value is
4747
* populated to the variable.
4848
*/
49-
@interface SHModelObject : NSObject <SHModelSerialization>
49+
@interface SHModelObject : NSObject <SHModelSerialization, NSCoding>
5050

5151
/**
5252
* class initializer

SHModelObject/SHModelObject/SHModelObject.m

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,54 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary dateConversionOpti
109109
return self;
110110
}
111111

112+
// NSCoding
113+
- (NSArray *)propertyNames
114+
{
115+
NSMutableArray *array = [NSMutableArray array];
116+
117+
//List of ivars
118+
id class = objc_getClass([NSStringFromClass([self class]) UTF8String]);
119+
_ivars = class_copyIvarList(class, &_outCount);
120+
121+
//If it match our ivar name, then set it
122+
for (unsigned int i = 0; i < _outCount; i++)
123+
{
124+
Ivar ivar = _ivars[i];
125+
NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
126+
[array addObject:ivarName];
127+
}
128+
129+
free(_ivars);
130+
_ivars = NULL;
131+
return array;
132+
}
133+
134+
- (id)initWithCoder:(NSCoder *)aDecoder
135+
{
136+
if ((self = [self init]))
137+
{
138+
// Loop through the properties
139+
for (NSString *key in [self propertyNames])
140+
{
141+
// Decode the property, and use the KVC setValueForKey: method to set it
142+
id value = [aDecoder decodeObjectForKey:key];
143+
[self setValue:value forKey:key];
144+
}
145+
}
146+
return self;
147+
}
148+
149+
- (void)encodeWithCoder:(NSCoder *)aCoder
150+
{
151+
// Loop through the properties
152+
for (NSString *key in [self propertyNames])
153+
{
154+
// Use the KVC valueForKey: method to get the property and then encode it
155+
id value = [self valueForKey:key];
156+
[aCoder encodeObject:value forKey:key];
157+
}
158+
}
159+
112160
//
113161
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary;
114162
{

0 commit comments

Comments
 (0)