Created
January 1, 2021 01:16
-
-
Save kasimte/b29c84cd29ceb03815b3db7ee2b06855 to your computer and use it in GitHub Desktop.
Swift Array extension to remove uncodable elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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