From 3842858443977839544c9c62a046e8c60646cc96 Mon Sep 17 00:00:00 2001 From: panxi Date: Fri, 13 Dec 2019 11:00:23 -0500 Subject: [PATCH] add encodeArrayIfPresent --- MVMCore/MVMCore/Models/Model/ModelRegistry.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 5157d84..f097004 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -127,12 +127,17 @@ extension KeyedEncodingContainer where Key: CodingKey { } ///need instance type as input paramaeter list - public mutating func encodeArray(_ list:[Model]?, forKey key:KeyedEncodingContainer.Key) throws { + public mutating func encodeArray(_ list:[Model], forKey key:KeyedEncodingContainer.Key) throws { var unkeyedContainer = self.nestedUnkeyedContainer(forKey: key) - try list?.forEach({ (model) in + for model in list { let typeString = type(of: model).identifier let type = ModelRegistry.getType(for: typeString) try type?.mvmencode(unkeyedContainer: &unkeyedContainer, model: model) - }) + } + } + + public mutating func encodeArrayIfPresent(_ list:[Model]?, forKey key:KeyedEncodingContainer.Key) throws { + guard let models = list else { return } + try self.encodeArray(models, forKey: key) } }