diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 1bf2a89..3e3fc3d 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -100,36 +100,13 @@ extension KeyedDecodingContainer where Key: CodingKey { } i += 1 } - return models.count > 0 ? models : nil + return models } public func decodeArray(codingKey: KeyedDecodingContainer.Key, typeCodingKey: C) throws -> [Model] { - var unkeyedContainer: UnkeyedDecodingContainer? - do { - unkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey) - } catch { + guard let models: [Model] = try decodeArrayIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else { throw ModelRegistry.Error.decoderErrorObjectNotPresent } - var otherUnkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey) - var attributes = [Model.Type]() - while !unkeyedContainer!.isAtEnd { - let meta = try unkeyedContainer!.nestedContainer(keyedBy: C.self) - let type = try meta.decode(String.self, forKey: typeCodingKey) - //get the type - guard let found = ModelRegistry.types[type] else { - throw ModelRegistry.Error.decoderErrorModelNotMapped - } - attributes.append(found) - } - var i = 0 - var models = [Model]() - while !otherUnkeyedContainer.isAtEnd { - let foundModel = attributes[i] - if let model = try foundModel.mvmdecode(unkeyedContainer: &otherUnkeyedContainer) { - models.append(model) - } - i += 1 - } return models } }