prevent nil

This commit is contained in:
panxi 2019-12-13 13:59:28 -05:00
parent ffc71ae0e2
commit 12e06894de

View File

@ -100,36 +100,13 @@ extension KeyedDecodingContainer where Key: CodingKey {
}
i += 1
}
return models.count > 0 ? models : nil
return models
}
public func decodeArray<C:CodingKey>(codingKey: KeyedDecodingContainer<K>.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
}
}