Guest User

Untitled

a guest
Apr 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. extension KeyedDecodingContainer {
  2.  
  3. /// Decode a heterogeneous list of objects for a given family.
  4. /// - Parameters:
  5. /// - heterogeneousType: The decodable type of the list.
  6. /// - family: The ClassFamily enum for the type family.
  7. /// - key: The CodingKey to look up the list in the current container.
  8. /// - Returns: The resulting list of heterogeneousType elements.
  9. func decode<T : Decodable, U : ClassFamily>(_ heterogeneousType: [T].Type, ofFamily family: U.Type, forKey key: K) throws -> [T] {
  10. var container = try self.nestedUnkeyedContainer(forKey: key)
  11. var list = [T]()
  12. var tmpContainer = container
  13. while !container.isAtEnd {
  14. let typeContainer = try container.nestedContainer(keyedBy: Discriminator.self)
  15. let family: U = try typeContainer.decode(U.self, forKey: U.discriminator)
  16. if let type = family.getType() as? T.Type {
  17. list.append(try tmpContainer.decode(type))
  18. }
  19. }
  20. return list
  21. }
  22. }
Add Comment
Please, Sign In to add comment