Created
April 17, 2018 06:31
-
-
Save jakubpetrik/d99e137a98be4442bbf369e08666cf0d to your computer and use it in GitHub Desktop.
Sequence+CompactCast
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 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