I would use compactMap instead of filter to increase type safety. You can use conditional reduction to filter out the elements you want and generics to save type information. This exploits the fact that compactMap can filter nil results from the conversion function.
let array: [VariousThings] = [ThingType1(), ThingType2()] func itemsMatchingType<T : VariousThings>(_ type: T.Type) -> [T] { return array.compactMap { $0 as? T } } let justThingTypes1 = itemsMatchingType(ThingType1.self) // of type [ThingType1]
Now the array that you get from your itemsMatchingType function is [ThingType1] if you pass ThingType1 , and not just [VariousThings] . This way, you wonโt have to deal with ugly forced downs later.
source share