Skip to content

Instantly share code, notes, and snippets.

@Kdan
Last active February 18, 2019 22:06
Show Gist options
  • Save Kdan/ca719621348836209ee66b30f0cb7d3d to your computer and use it in GitHub Desktop.
Save Kdan/ca719621348836209ee66b30f0cb7d3d to your computer and use it in GitHub Desktop.
extension JSONDecoder {
/// Decode a heterogeneous list of objects.
/// - Parameters:
/// - family: The ClassFamily enum type to decode with.
/// - data: The data to decode.
/// - Returns: The list of decoded objects.
func decode<T: ClassFamily, U: Decodable>(family: T.Type, from data: Data) throws -> [U] {
return try self.decode([ClassWrapper<T, U>].self, from: data).compactMap { $0.object }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment