From 5d836fc7574231e3221ba2c279308d6255b80287 Mon Sep 17 00:00:00 2001 From: panxi Date: Fri, 13 Dec 2019 15:05:07 -0500 Subject: [PATCH] decodeIfPresent if nil --- MVMCore/MVMCore/Models/Model/ModelRegistry.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 3e3fc3d..2a3ebb2 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -45,10 +45,14 @@ extension KeyedDecodingContainer where Key: CodingKey { //MARK: - DecodeIfPresent public func decodeIfPresent(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> T? { - //get the type string - let meta = try self.nestedContainer(keyedBy: C.self, forKey: codingKey) - guard let type = try meta.decodeIfPresent(String.self, forKey: typeCodingKey) else { + var me: KeyedDecodingContainer? + do { + me = try self.nestedContainer(keyedBy: C.self, forKey: codingKey) + } catch { + return nil + } + guard let type = try me?.decodeIfPresent(String.self, forKey: typeCodingKey) else { return nil }