add encodeArrayIfPresent

This commit is contained in:
panxi 2019-12-13 11:00:23 -05:00
parent a16c56505e
commit 3842858443

View File

@ -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<K>.Key) throws {
public mutating func encodeArray(_ list:[Model], forKey key:KeyedEncodingContainer<K>.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<K>.Key) throws {
guard let models = list else { return }
try self.encodeArray(models, forKey: key)
}
}