add encode method for model list

This commit is contained in:
panxi 2019-12-13 10:17:20 -05:00
parent eb4e8fd816
commit 5fcbe414ec

View File

@ -125,4 +125,12 @@ extension KeyedEncodingContainer where Key: CodingKey {
let encoder = self.superEncoder(forKey: key)
try value.encode(to: encoder)
}
///need instance type as input paramaeter list
public mutating func encodeArray<T:Model>(_ list:[T]?, forKey key:KeyedEncodingContainer<K>.Key) throws {
var unkeyedContainer = self.nestedUnkeyedContainer(forKey: key)
try list?.forEach({ (model) in
try unkeyedContainer.encode(model)
})
}
}