With Swift 5, you can choose one of the following Playground sample codes in order to solve your problem.
let array: [Any] = [5, "a", 6]
for case let item as Int in array {
print(item) // item is of type Int here
}
/*
prints:
5
6
*/
let array: [Any] = [5, "a", 6]
for item in array.compactMap({ $0 as? Int }) {
print(item) // item is of type Int here
}
/*
prints:
5
6
*/
let array: [Any] = [5, "a", 6]
for item in array where item is Int {
print(item) // item conforms to Any protocol here
}
/*
prints:
5
6
*/
let array: [Any] = [5, "a", 6]
for item in array.filter({ $0 is Int }) {
print(item) // item conforms to Any protocol here
}
/*
prints:
5
6
*/