@@ -48,16 +48,16 @@ public enum JSONDecodableError: ErrorType, CustomStringConvertible {
4848// Dictionary -> Struct
4949
5050public protocol JSONDecodable {
51- init ? ( JSONDictionary: JSONObject )
51+ init ( JSONDictionary: JSONObject ) throws
5252}
5353
5454public extension Array where Element: JSONDecodable {
55- init ( JSONArray: [ AnyObject ] ) {
56- self . init ( JSONArray . flatMap {
55+ init ( JSONArray: [ AnyObject ] ) throws {
56+ self . init ( try JSONArray . flatMap {
5757 guard let json = $0 as? [ String : AnyObject ] else {
58- return nil
58+ throw JSONDecodableError . DictionaryTypeExpectedError ( key : " [inarray] " , elementType : $0 . dynamicType )
5959 }
60- return Element ( JSONDictionary: json)
60+ return try Element ( JSONDictionary: json)
6161 } )
6262 }
6363}
@@ -110,10 +110,7 @@ public class JSONDecoder {
110110 guard let object = value as? JSONObject else {
111111 throw JSONDecodableError . DictionaryTypeExpectedError ( key: key, elementType: value. dynamicType)
112112 }
113- guard let decodable = Decodable ( JSONDictionary: object) else {
114- throw JSONDecodableError . IncompatibleTypeError ( key: key, elementType: value. dynamicType, expectedType: Decodable . self)
115- }
116- return decodable
113+ return try Decodable ( JSONDictionary: object)
117114 }
118115
119116 // JSONDecodable?
@@ -124,7 +121,7 @@ public class JSONDecoder {
124121 guard let object = value as? JSONObject else {
125122 throw JSONDecodableError . DictionaryTypeExpectedError ( key: key, elementType: value. dynamicType)
126123 }
127- return Decodable ( JSONDictionary: object)
124+ return try Decodable ( JSONDictionary: object)
128125 }
129126
130127 // Enum
@@ -185,7 +182,7 @@ public class JSONDecoder {
185182 guard let array = value as? [ JSONObject ] else {
186183 throw JSONDecodableError . ArrayTypeExpectedError ( key: key, elementType: value. dynamicType)
187184 }
188- return array. flatMap { Element ( JSONDictionary: $0) }
185+ return try array. flatMap { try Element ( JSONDictionary: $0) }
189186 }
190187
191188 // [JSONDecodable]?
@@ -196,7 +193,7 @@ public class JSONDecoder {
196193 guard let array = value as? [ JSONObject ] else {
197194 throw JSONDecodableError . ArrayTypeExpectedError ( key: key, elementType: value. dynamicType)
198195 }
199- return array. flatMap { Element ( JSONDictionary: $0) }
196+ return try array. flatMap { try Element ( JSONDictionary: $0) }
200197 }
201198
202199 // [Enum]
0 commit comments