Skip to content

Commit bcc8077

Browse files
committed
added functionality to auto parse SHModelObject variables and NSArrays or SHModelObjects provided mapping
1 parent 60c4f8a commit bcc8077

File tree

10 files changed

+126
-27
lines changed

10 files changed

+126
-27
lines changed

SHModelObject.xcodeproj/project.pbxproj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
F244007518DACD6B0078B6B0 /* SHModelObject.m in Sources */ = {isa = PBXBuildFile; fileRef = F244006F18DACD6B0078B6B0 /* SHModelObject.m */; };
1111
F244007618DACD6B0078B6B0 /* SHAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F244007118DACD6B0078B6B0 /* SHAppDelegate.m */; };
1212
F244007718DACD6B0078B6B0 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F244007218DACD6B0078B6B0 /* Images.xcassets */; };
13-
F244007818DACD6B0078B6B0 /* SHTestModal.m in Sources */ = {isa = PBXBuildFile; fileRef = F244007418DACD6B0078B6B0 /* SHTestModal.m */; };
1413
F2B4093118BA16F400611B29 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F2B4093018BA16F400611B29 /* Foundation.framework */; };
1514
F2B4093318BA16F400611B29 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F2B4093218BA16F400611B29 /* CoreGraphics.framework */; };
1615
F2B4093518BA16F400611B29 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F2B4093418BA16F400611B29 /* UIKit.framework */; };
@@ -21,6 +20,8 @@
2120
F2B4094C18BA16F500611B29 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F2B4093418BA16F400611B29 /* UIKit.framework */; };
2221
F2B4095418BA16F500611B29 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F2B4095218BA16F500611B29 /* InfoPlist.strings */; };
2322
F2B4095618BA16F500611B29 /* SHModalObjectTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F2B4095518BA16F500611B29 /* SHModalObjectTests.m */; };
23+
F2FA97791AB40312002B972D /* SHAnotherModel.m in Sources */ = {isa = PBXBuildFile; fileRef = F2FA97781AB40312002B972D /* SHAnotherModel.m */; };
24+
F2FA977C1AB40377002B972D /* SHTestModal.m in Sources */ = {isa = PBXBuildFile; fileRef = F2FA977B1AB40377002B972D /* SHTestModal.m */; };
2425
/* End PBXBuildFile section */
2526

2627
/* Begin PBXContainerItemProxy section */
@@ -40,7 +41,6 @@
4041
F244007118DACD6B0078B6B0 /* SHAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SHAppDelegate.m; sourceTree = "<group>"; };
4142
F244007218DACD6B0078B6B0 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
4243
F244007318DACD6B0078B6B0 /* SHAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SHAppDelegate.h; sourceTree = "<group>"; };
43-
F244007418DACD6B0078B6B0 /* SHTestModal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SHTestModal.m; sourceTree = "<group>"; };
4444
F2B4092D18BA16F400611B29 /* SHModelObject.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SHModelObject.app; sourceTree = BUILT_PRODUCTS_DIR; };
4545
F2B4093018BA16F400611B29 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
4646
F2B4093218BA16F400611B29 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -54,6 +54,10 @@
5454
F2B4095118BA16F500611B29 /* SHModelObjectTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SHModelObjectTests-Info.plist"; sourceTree = "<group>"; };
5555
F2B4095318BA16F500611B29 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
5656
F2B4095518BA16F500611B29 /* SHModalObjectTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SHModalObjectTests.m; sourceTree = "<group>"; };
57+
F2FA97771AB40312002B972D /* SHAnotherModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SHAnotherModel.h; sourceTree = "<group>"; };
58+
F2FA97781AB40312002B972D /* SHAnotherModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SHAnotherModel.m; sourceTree = "<group>"; };
59+
F2FA977A1AB40377002B972D /* SHTestModal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SHTestModal.h; sourceTree = "<group>"; };
60+
F2FA977B1AB40377002B972D /* SHTestModal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SHTestModal.m; sourceTree = "<group>"; };
5761
/* End PBXFileReference section */
5862

5963
/* Begin PBXFrameworksBuildPhase section */
@@ -126,9 +130,12 @@
126130
F244006D18DACD6B0078B6B0 /* SHModelObject */,
127131
F244007118DACD6B0078B6B0 /* SHAppDelegate.m */,
128132
F244007318DACD6B0078B6B0 /* SHAppDelegate.h */,
129-
F244007418DACD6B0078B6B0 /* SHTestModal.m */,
130133
F244007218DACD6B0078B6B0 /* Images.xcassets */,
131134
F2B4093718BA16F400611B29 /* Supporting Files */,
135+
F2FA977A1AB40377002B972D /* SHTestModal.h */,
136+
F2FA977B1AB40377002B972D /* SHTestModal.m */,
137+
F2FA97771AB40312002B972D /* SHAnotherModel.h */,
138+
F2FA97781AB40312002B972D /* SHAnotherModel.m */,
132139
);
133140
path = SHModelObject;
134141
sourceTree = "<group>";
@@ -258,10 +265,11 @@
258265
isa = PBXSourcesBuildPhase;
259266
buildActionMask = 2147483647;
260267
files = (
261-
F244007818DACD6B0078B6B0 /* SHTestModal.m in Sources */,
268+
F2FA97791AB40312002B972D /* SHAnotherModel.m in Sources */,
262269
F2B4093D18BA16F400611B29 /* main.m in Sources */,
263270
F244007518DACD6B0078B6B0 /* SHModelObject.m in Sources */,
264271
F244007618DACD6B0078B6B0 /* SHAppDelegate.m in Sources */,
272+
F2FA977C1AB40377002B972D /* SHTestModal.m in Sources */,
265273
);
266274
runOnlyForDeploymentPostprocessing = 0;
267275
};

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
<string>SHModelObject</string>
1111
<key>IDESourceControlProjectOriginsDictionary</key>
1212
<dict>
13-
<key>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</key>
14-
<string>https://github.com/ndduy/SHModelObject.git</string>
13+
<key>B5D04170A948972EE1EFA3B27A34E674C9EF57CA</key>
14+
<string>https://github.com/grevolution/SHModelObject.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
17-
<string>SHModelObject.xcodeproj/project.xcworkspace</string>
17+
<string>SHModelObject.xcodeproj</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
20-
<key>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</key>
20+
<key>B5D04170A948972EE1EFA3B27A34E674C9EF57CA</key>
2121
<string>../..</string>
2222
</dict>
2323
<key>IDESourceControlProjectURL</key>
24-
<string>https://github.com/ndduy/SHModelObject.git</string>
24+
<string>https://github.com/grevolution/SHModelObject.git</string>
2525
<key>IDESourceControlProjectVersion</key>
26-
<integer>110</integer>
26+
<integer>111</integer>
2727
<key>IDESourceControlProjectWCCIdentifier</key>
28-
<string>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</string>
28+
<string>B5D04170A948972EE1EFA3B27A34E674C9EF57CA</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>454FEFC7-BFFF-4DA5-B089-6B427C2C7510</string>
35+
<string>B5D04170A948972EE1EFA3B27A34E674C9EF57CA</string>
3636
<key>IDESourceControlWCCName</key>
3737
<string>SHModelObject</string>
3838
</dict>

SHModelObject.xcodeproj/xcuserdata/shanulhaq.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
shouldBeEnabled = "Yes"
1010
ignoreCount = "0"
1111
continueAfterRunningActions = "No"
12-
filePath = "SHModelObject/SHAppDelegate.m"
13-
timestampString = "427600078.010373"
12+
filePath = "SHModelObject/SHModelObject/SHModelObject.m"
13+
timestampString = "448012336.337347"
1414
startingColumnNumber = "9223372036854775807"
1515
endingColumnNumber = "9223372036854775807"
16-
startingLineNumber = "60"
17-
endingLineNumber = "60"
18-
landmarkName = "-application:didFinishLaunchingWithOptions:"
16+
startingLineNumber = "308"
17+
endingLineNumber = "308"
18+
landmarkName = "-serializeValue:withKey:"
1919
landmarkType = "5">
2020
</BreakpointContent>
2121
</BreakpointProxy>
@@ -25,13 +25,13 @@
2525
shouldBeEnabled = "Yes"
2626
ignoreCount = "0"
2727
continueAfterRunningActions = "No"
28-
filePath = "SHModelObject/SHModelObject/SHModelObject.m"
29-
timestampString = "427600428.987373"
28+
filePath = "SHModelObject/SHAppDelegate.m"
29+
timestampString = "448012488.962849"
3030
startingColumnNumber = "9223372036854775807"
3131
endingColumnNumber = "9223372036854775807"
32-
startingLineNumber = "188"
33-
endingLineNumber = "188"
34-
landmarkName = "-serializeValue:withKey:"
32+
startingLineNumber = "80"
33+
endingLineNumber = "80"
34+
landmarkName = "-application:didFinishLaunchingWithOptions:"
3535
landmarkType = "5">
3636
</BreakpointContent>
3737
</BreakpointProxy>

SHModelObject/SHAnotherModel.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// SHAnotherModel.h
3+
// SHModelObject
4+
//
5+
// Created by SHAN UL HAQ on 14/3/15.
6+
// Copyright (c) 2015 grevolution. All rights reserved.
7+
//
8+
9+
#import "SHModelObject.h"
10+
11+
@interface SHAnotherModel : SHModelObject
12+
13+
@property (nonatomic) int modelId;
14+
@property (nonatomic, strong) NSString *modelName;
15+
@property (nonatomic, strong) NSString *modelType;
16+
17+
@end

SHModelObject/SHAnotherModel.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// SHAnotherModel.m
3+
// SHModelObject
4+
//
5+
// Created by SHAN UL HAQ on 14/3/15.
6+
// Copyright (c) 2015 grevolution. All rights reserved.
7+
//
8+
9+
#import "SHAnotherModel.h"
10+
11+
@implementation SHAnotherModel
12+
13+
@end

SHModelObject/SHAppDelegate.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,29 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5050
@"time1" : @"2014-04-11T23:59:00+08:00",
5151
@"time2" : @"2014-04-11T23:59:00+08:00",
5252
@"time3" : @"2014-04-11T23:59:00+08:00",
53+
@"anotherModel" : @{
54+
@"modelId" : @1,
55+
@"modelName" : @"My Model",
56+
@"modelType" : @"My Model Type"
57+
},
58+
@"arrayOfAnotherModels" : @[
59+
@{
60+
@"modelId" : @2,
61+
@"modelName" : @"My Model 2",
62+
@"modelType" : @"My Model Type 2"
63+
},@{
64+
@"modelId" : @3,
65+
@"modelName" : @"My Model 3",
66+
@"modelType" : @"My Model Type 3"
67+
},@{
68+
@"modelId" : @4,
69+
@"modelName" : @"My Model 4",
70+
@"modelType" : @"My Model Type 4"
71+
}
72+
]
5373
};
5474

55-
NSDictionary *mapping = @{@"time1":@"",@"time2":@"",@"time3":@""};
75+
NSDictionary *mapping = @{@"time1":@"",@"time2":@"",@"time3":@"", @"arrayOfAnotherModels" : @"SHAnotherModel"};
5676
SHTestModal *modal = [[SHTestModal alloc] initWithDictionary:a
5777
dateConversionOption:kDateConverstionFromNSStringToNSDateOption
5878
inputDateType:kInputDateFormatDotNetWithTimeZone mappings:mapping];

SHModelObject/SHModelObject-Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0</string>
20+
<string>1.0.8</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleVersion</key>
24-
<string>1.0</string>
24+
<string>1.0.8</string>
2525
<key>LSRequiresIPhoneOS</key>
2626
<true/>
2727
<key>UIRequiredDeviceCapabilities</key>

SHModelObject/SHModelObject/SHModelObject.m

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,56 @@ - (void) serializeValue:(id)value withKey:(id)key;
291291
NSAssert(false, @"the types do not match : %@ vs %@", ivarType, @"NSNumber");
292292
}
293293
} else if([value isKindOfClass:[NSDictionary class]]) {
294-
if(![ivarType contains:@"Dictionary"]) {
294+
NSString *typeName = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
295+
typeName = [typeName stringByReplacingOccurrencesOfString:@"\"" withString:@""];
296+
Class objectClass = NSClassFromString(typeName);
297+
BOOL isSHModelObject = [self isSHModelObject:objectClass];
298+
if(isSHModelObject){
299+
value = [objectClass objectWithDictionary:value];
300+
} else if(![ivarType contains:@"Dictionary"]) {
295301
NSAssert(false, @"the types do not match : %@ vs %@", ivarType, @"NSDictionary or NSMutableDictionary");
296302
}
297303
} else if([value isKindOfClass:[NSArray class]]) {
298304
if(![ivarType contains:@"Array"]) {
299305
NSAssert(false, @"the types do not match : %@ vs %@", ivarType, @"NSArray or NSMutableArray");
300306
}
307+
308+
id availableMappingClass = _dateValueMappings[key];
309+
if(availableMappingClass && [availableMappingClass isKindOfClass:[NSString class]]) {
310+
//mapping string available.
311+
Class objectClass = NSClassFromString(availableMappingClass);
312+
BOOL isSHModelObject = [self isSHModelObject:objectClass];
313+
if(isSHModelObject){
314+
NSMutableArray *valueArray = [NSMutableArray arrayWithCapacity:[value count]];
315+
for(id item in value){
316+
//item should be a dictionary
317+
if([item isKindOfClass:[NSDictionary class]]) {
318+
id itemObject = [objectClass objectWithDictionary:item];
319+
if(itemObject){
320+
[valueArray addObject:itemObject];
321+
}
322+
} else {
323+
NSLog(@"object %@ is not a NSDictionary object, skipping.", [item description]);
324+
}
325+
}
326+
value = valueArray;
327+
}
328+
}
301329
}
302330

303331
[self setValue:value forKey:ivarName];
304332
}
305333
}
306334

335+
- (BOOL)isSHModelObject:(Class)class {
336+
if(class == nil)
337+
return NO;
338+
if(class == [SHModelObject class]){
339+
return YES;
340+
}
341+
return [self isSHModelObject:[class superclass]];
342+
}
343+
307344
#pragma mark - NSObject overriden methods
308345

309346
//

SHModelObject/SHTestModal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#import "SHModelObject.h"
2424

25+
@class SHAnotherModel;
26+
2527
@interface SHTestModal : SHModelObject
2628

2729
{
@@ -34,13 +36,15 @@
3436

3537
NSMutableArray *_myArray;
3638
NSMutableDictionary *_myDictionary;
37-
3839
NSDate *time1;
3940
}
4041

4142
@property (nonatomic, strong) NSString *stringValue;
4243
@property (nonatomic) int anotherIntValue;
4344
@property (nonatomic) NSInteger anotherIntegerValue;
4445
@property (nonatomic, strong) NSNumber *anotherNumberValue;
46+
@property (nonatomic, strong) SHAnotherModel *anotherModel;
47+
@property (nonatomic, strong) NSArray *arrayOfAnotherModels;
48+
4549

4650
@end

0 commit comments

Comments
 (0)