diff --git a/SwiftyJSONModel/JSONObject.swift b/SwiftyJSONModel/JSONObject.swift index 33e2c83..cc6fd69 100644 --- a/SwiftyJSONModel/JSONObject.swift +++ b/SwiftyJSONModel/JSONObject.swift @@ -43,22 +43,6 @@ public extension JSONObject where PropertyType.RawValue == String { } // MARK: - Throwable methods - public func value(for key: PropertyType) throws -> T { - do { - return try T(json: self[key]) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) - } - } - - public func value(for key: PropertyType) throws -> [T] { - do { - return try self[key].arrayValue().map({ try T(json: $0) }) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) - } - } - public func object(for key: PropertyType) throws -> JSONObject { do { return try JSONObject(json: self[key]) @@ -73,16 +57,17 @@ public extension JSONObject where PropertyType.RawValue == String { public func value(for keyPath: [PropertyType]) throws -> T { assert(keyPath.isEmpty == false, "KeyPath cannot be empty") + let key = keyPath[0] - if keyPath.count == 1 { - return try value(for: key) - } else { - let subPath: [PropertyType] = .init(keyPath[1..(json: self[key]).value(for: subPath) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) } + } catch let error as JSONModelError { + throw JSONModelError.invalidValueFor(key: key.rawValue, error) } } @@ -92,28 +77,21 @@ public extension JSONObject where PropertyType.RawValue == String { public func value(for keyPath: [PropertyType]) throws -> [T] { assert(keyPath.isEmpty == false, "KeyPath cannot be empty") + let key = keyPath[0] - if keyPath.count == 1 { - return try value(for: key) - } else { - let subPath: [PropertyType] = .init(keyPath[1..(json: self[key]).value(for: subPath) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) } + } catch let error as JSONModelError { + throw JSONModelError.invalidValueFor(key: key.rawValue, error) } } // MARK: - Optional methods - public func value(for key: PropertyType) -> T? { - return try? value(for: key) - } - - public func value(for key: PropertyType) -> [T]? { - return try? value(for: key) - } - public func value(for keyPath: PropertyType...) -> T? { return try? value(for: keyPath) }