Skip to content

Instantly share code, notes, and snippets.

@lightsprint09
Created May 26, 2016 16:24
Show Gist options
  • Save lightsprint09/32e7e0c4e384e5cfa684896e78d12e18 to your computer and use it in GitHub Desktop.
Save lightsprint09/32e7e0c4e384e5cfa684896e78d12e18 to your computer and use it in GitHub Desktop.
extension Array {
/**
Hacky way to may a Array with Type Elemnet to an arry with Supertype T. T must be Supertype of Array.Element
- returns: array of type T
*/
func mapType<T>() -> Array<T> {
let mappedCategorys: Array<T> = self.map { return $0 as! T }
return mappedCategorys
}
}
//Usage
protocol Foo {}
struct Bar: Foo {}
let listOfBar = [Bar(), Bar()]
let listOfFoo: Array<Foo> = listOfBar.mapType()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment