prevent nil
This commit is contained in:
parent
ffc71ae0e2
commit
12e06894de
@ -100,36 +100,13 @@ extension KeyedDecodingContainer where Key: CodingKey {
|
|||||||
}
|
}
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
return models.count > 0 ? models : nil
|
return models
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decodeArray<C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model] {
|
public func decodeArray<C:CodingKey>(codingKey: KeyedDecodingContainer<K>.Key, typeCodingKey: C) throws -> [Model] {
|
||||||
var unkeyedContainer: UnkeyedDecodingContainer?
|
guard let models: [Model] = try decodeArrayIfPresent(codingKey: codingKey, typeCodingKey: typeCodingKey) else {
|
||||||
do {
|
|
||||||
unkeyedContainer = try nestedUnkeyedContainer(forKey: codingKey)
|
|
||||||
} catch {
|
|
||||||
throw ModelRegistry.Error.decoderErrorObjectNotPresent
|
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
|
return models
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user