Condensing catch

This commit is contained in:
Pfeil, Scott Robert 2019-12-18 13:24:11 -05:00
parent e1ecb35531
commit 326f761f4f

View File

@ -49,13 +49,7 @@ extension KeyedDecodingContainer where Key: CodingKey {
/// Decodes to a registered model based on the identifier, optional.
public func decodeModelIfPresent<T, C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> T? {
//get the identifier string
var container: KeyedDecodingContainer<C>?
do {
container = try nestedContainer(keyedBy: C.self, forKey: codingKey)
} catch {
return nil
}
guard let identifier = try container?.decodeIfPresent(String.self, forKey: typeCodingKey) else {
guard let container = try? nestedContainer(keyedBy: C.self, forKey: codingKey), let identifier = try? container.decodeIfPresent(String.self, forKey: typeCodingKey) else {
return nil
}
@ -77,8 +71,10 @@ extension KeyedDecodingContainer where Key: CodingKey {
/// Decodes an array of registered model based on the identifiers, optional.
public func decodeModelsIfPresent<C: CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model]? {
guard var container = try? nestedUnkeyedContainer(forKey: codingKey) else {
return nil
}
var models = [Model]()
var container = try nestedUnkeyedContainer(forKey: codingKey)
var containerCopy = container
while !container.isAtEnd {
let nestedContainer = try container.nestedContainer(keyedBy: C.self)