@@ -48,7 +48,17 @@ public enum JSONDecodableError: ErrorType, CustomStringConvertible {
4848// Dictionary -> Struct
4949
5050public protocol JSONDecodable {
51- init ( JSONDictionary: JSONObject ) throws
51+ init ( object: JSONObject ) throws
52+ }
53+
54+ public extension JSONDecodable {
55+ init ? ( JSONDictionary: JSONObject ) {
56+ do {
57+ try self . init ( object: JSONDictionary)
58+ } catch {
59+ return nil
60+ }
61+ }
5262}
5363
5464public extension Array where Element: JSONDecodable {
@@ -57,7 +67,7 @@ public extension Array where Element: JSONDecodable {
5767 guard let json = $0 as? [ String : AnyObject ] else {
5868 throw JSONDecodableError . DictionaryTypeExpectedError ( key: " [inarray] " , elementType: $0. dynamicType)
5969 }
60- return try Element ( JSONDictionary : json)
70+ return try Element ( object : json)
6171 } )
6272 }
6373}
@@ -110,7 +120,7 @@ public class JSONDecoder {
110120 guard let object = value as? JSONObject else {
111121 throw JSONDecodableError . DictionaryTypeExpectedError ( key: key, elementType: value. dynamicType)
112122 }
113- return try Decodable ( JSONDictionary : object)
123+ return try Decodable ( object : object)
114124 }
115125
116126 // JSONDecodable?
@@ -121,7 +131,7 @@ public class JSONDecoder {
121131 guard let object = value as? JSONObject else {
122132 throw JSONDecodableError . DictionaryTypeExpectedError ( key: key, elementType: value. dynamicType)
123133 }
124- return try Decodable ( JSONDictionary : object)
134+ return try Decodable ( object : object)
125135 }
126136
127137 // Enum
@@ -182,7 +192,7 @@ public class JSONDecoder {
182192 guard let array = value as? [ JSONObject ] else {
183193 throw JSONDecodableError . ArrayTypeExpectedError ( key: key, elementType: value. dynamicType)
184194 }
185- return try array. flatMap { try Element ( JSONDictionary : $0) }
195+ return try array. flatMap { try Element ( object : $0) }
186196 }
187197
188198 // [JSONDecodable]?
@@ -193,7 +203,7 @@ public class JSONDecoder {
193203 guard let array = value as? [ JSONObject ] else {
194204 throw JSONDecodableError . ArrayTypeExpectedError ( key: key, elementType: value. dynamicType)
195205 }
196- return try array. flatMap { try Element ( JSONDictionary : $0) }
206+ return try array. flatMap { try Element ( object : $0) }
197207 }
198208
199209 // [Enum]
0 commit comments