Skip to content

Instantly share code, notes, and snippets.

@erica
Created May 4, 2015 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/19be5f458a40b909f9df to your computer and use it in GitHub Desktop.
Save erica/19be5f458a40b909f9df to your computer and use it in GitHub Desktop.
/// Fetch item by class
public func SelectTypedItemFromArray<T, U: SequenceType>(type t: T.Type, array: U) -> T? {
for eachItem in array {
if let typedItem = eachItem as? T {
return typedItem
}
}
return nil
}
// Filter array by class
public func FilterTypeFromArray<T, U>(type t: T.Type, array: Array<U>) -> [T] {
return array.flatMap{
if let item = $0 as? T {
return [item]
} else { return [] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment