Merge pull request #2090 from jaz-ah/issue-1994

[Swift] fix issue 1994: make sure we don't crash if rawValue returns …
This commit is contained in:
wing328 2016-02-10 13:54:57 +08:00
commit 3ba4cd1409
2 changed files with 3 additions and 3 deletions

View File

@ -126,7 +126,7 @@ class Decoders {
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
let sourceDictionary = source as! [NSObject:AnyObject]
let instance = {{classname}}(){{#vars}}{{#isEnum}}
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
instance.{{name}} = {{classname}}.{{datatypeWithEnum}}(rawValue: (sourceDictionary["{{name}}"] as? String) ?? ""){{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
instance.{{name}} = Decoders.decode{{^unwrapRequired}}Optional{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}Optional{{/required}}{{/unwrapRequired}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}}){{/isEnum}}{{/vars}}
return instance
}{{/model}}

View File

@ -165,7 +165,7 @@ class Decoders {
instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
instance.photoUrls = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["photoUrls"])
instance.tags = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["tags"])
instance.status = (sourceDictionary["status"] as? String).map { Pet.Status(rawValue: $0)! }
instance.status = Pet.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
return instance
}
@ -196,7 +196,7 @@ class Decoders {
instance.petId = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["petId"])
instance.quantity = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["quantity"])
instance.shipDate = Decoders.decodeOptional(clazz: NSDate.self, source: sourceDictionary["shipDate"])
instance.status = (sourceDictionary["status"] as? String).map { Order.Status(rawValue: $0)! }
instance.status = Order.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
instance.complete = Decoders.decodeOptional(clazz: Bool.self, source: sourceDictionary["complete"])
return instance
}