Skip to content

Commit d25ca35

Browse files
committed
Add init? for backward compatibility
1 parent df58c58 commit d25ca35

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

JSONCodable.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
9EDB390B1B59D00B00C63019 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6161
9EDB39231B59D01D00C63019 /* JSONCodable.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSONCodable.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6262
9EDB393C1B59D0AF00C63019 /* JSONCodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONCodable.swift; sourceTree = "<group>"; };
63-
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; };
63+
9EDB393D1B59D0AF00C63019 /* JSONDecodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = JSONDecodable.swift; sourceTree = "<group>"; tabWidth = 4; };
6464
9EDB393E1B59D0AF00C63019 /* JSONEncodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONEncodable.swift; sourceTree = "<group>"; };
6565
9EDB393F1B59D0AF00C63019 /* JSONHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHelpers.swift; sourceTree = "<group>"; };
6666
9EDB39411B59D0AF00C63019 /* JSONString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONString.swift; sourceTree = "<group>"; };

JSONCodable/JSONDecodable.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ public enum JSONDecodableError: ErrorType, CustomStringConvertible {
4848
// Dictionary -> Struct
4949

5050
public 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

5464
public 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]

JSONCodable/JSONString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public extension JSONDecodable {
6767
throw JSONDecodableError.DictionaryTypeExpectedError(key: "n/a", elementType: result.dynamicType)
6868
}
6969

70-
try self.init(JSONDictionary: converted)
70+
try self.init(object: converted)
7171
}
7272
}
7373

0 commit comments

Comments
 (0)