Makes Monster initializer use new JSONHelper methods to make parsing more expressive.

This commit is contained in:
2020-09-17 13:03:48 -07:00
parent c224c51f84
commit c58f0909bb

View File

@@ -8,6 +8,7 @@
#import "Monster.h" #import "Monster.h"
#import "StringHelper.h" #import "StringHelper.h"
#import "JSONHelper.h"
@implementation Monster @implementation Monster
@@ -60,44 +61,22 @@
-(id)initWithJSONData: (NSData*)jsonData andContext:(NSManagedObjectContext*)context { -(id)initWithJSONData: (NSData*)jsonData andContext:(NSManagedObjectContext*)context {
self = [self initWithContext:context]; self = [self initWithContext:context];
NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; NSDictionary *jsonRoot = [JSONHelper parseJSONDataAsDictionary:jsonData];
NSNumber *tempNumber;
self.name = [jsonRoot objectForKey:@"name"] ?: @""; self.name = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"name" withDefaultValue:@""];
self.size = [jsonRoot objectForKey:@"size"] ?: @""; self.size = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"size" withDefaultValue:@""];
self.type = [jsonRoot objectForKey:@"type"] ?: @""; self.type = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"type" withDefaultValue:@""];
self.subtype = [jsonRoot objectForKey:@"tag"] ?: @""; self.subtype = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"tag" withDefaultValue:@""];
self.alignment = [jsonRoot objectForKey:@"alignment"] ?: @""; self.alignment = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"alignment" withDefaultValue:@""];
self.armorName = [jsonRoot objectForKey:@"armorName"] ?: @""; self.armorName = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"armorName" withDefaultValue:@""];
self.otherArmorDescription = [jsonRoot objectForKey:@"otherArmorDesc"] ?: @""; self.otherArmorDescription = [JSONHelper readStringFromDictionary:jsonRoot forKey:@"otherArmorDesc" withDefaultValue:@""];
tempNumber = [jsonRoot objectForKey:@"strPoints"]; self.strengthScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"strPoints" withDefaultValue:0];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) { self.dexterityScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"dexPoints" withDefaultValue:0];
self.strengthScore = tempNumber.intValue; self.constitutionScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"conPoints" withDefaultValue:0];
} self.intelligenceScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"intPoints" withDefaultValue:0];
tempNumber = [jsonRoot objectForKey:@"dexPoints"]; self.wisdomScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"wisPoints" withDefaultValue:0];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) { self.charismaScore = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"chaPoints" withDefaultValue:0];
self.dexterityScore = tempNumber.intValue; self.shieldBonus = [JSONHelper readIntFromDictionary:jsonRoot forKey:@"shieldBonus" withDefaultValue:0];
}
tempNumber = [jsonRoot objectForKey:@"conPoints"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.constitutionScore = tempNumber.intValue;
}
tempNumber = [jsonRoot objectForKey:@"intPoints"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.intelligenceScore = tempNumber.intValue;
}
tempNumber = [jsonRoot objectForKey:@"wisPoints"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.wisdomScore = tempNumber.intValue;
}
tempNumber = [jsonRoot objectForKey:@"chaPoints"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.charismaScore = tempNumber.intValue;
}
tempNumber = [jsonRoot objectForKey:@"shieldBonus"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.shieldBonus = tempNumber.intValue;
}
return self; return self;
} }