Skip to content

Instantly share code, notes, and snippets.

@kasimte
Created January 1, 2021 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasimte/b29c84cd29ceb03815b3db7ee2b06855 to your computer and use it in GitHub Desktop.
Save kasimte/b29c84cd29ceb03815b3db7ee2b06855 to your computer and use it in GitHub Desktop.
Swift Array extension to remove uncodable elements.
extension Array where Element: Encodable {
/// - Returns: An array having removed any element of the array which is not encodable itself.
func filterUncodable() -> [Element] {
return self.filter { (el: Element) -> Bool in
let encoded = try? JSONEncoder().encode(el)
return encoded != nil ? true : false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment