Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created February 13, 2018 15:08
Show Gist options
  • Select an option

  • Save DonMag/d13ce4dec6d95e7131d3afd6ecbb7647 to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/d13ce4dec6d95e7131d3afd6ecbb7647 to your computer and use it in GitHub Desktop.
import Foundation
class Foo {
var test = ""
}
class Bar {
var test = ""
}
var myArray = [AnyObject]()
for i in 0..<5 {
let f = Foo()
f.test = "Foo \(i)"
myArray.append(f)
let b = Bar()
b.test = "Bar \(i)"
myArray.append(b)
}
print("myArray has", myArray.count, "elements")
print(myArray)
print()
let fooArray = myArray.filter({
$0 is Foo
})
let barArray = myArray.filter({
$0 is Bar
})
print("fooArray has", fooArray.count, "elements")
print(fooArray)
print()
print("barArray has", barArray.count, "elements")
print(barArray)
print()
for f in fooArray {
if let obj = f as? Foo {
print(obj.test)
}
}
print()
for b in barArray {
if let obj = b as? Bar {
print(obj.test)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment