Skip to content

Instantly share code, notes, and snippets.

@jakubpetrik
Created April 17, 2018 06:31
Show Gist options
  • Save jakubpetrik/d99e137a98be4442bbf369e08666cf0d to your computer and use it in GitHub Desktop.
Save jakubpetrik/d99e137a98be4442bbf369e08666cf0d to your computer and use it in GitHub Desktop.
Sequence+CompactCast
extension Sequence {
func compactCast<T>(to type: T.Type) -> [T] {
return compactMap { $0 as? T }
}
}
// usage
let items = [1, 2, nil, 4].compactCast(to: Int.self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment