If you know the types of what you will store in advance, you can wrap them in an enumeration. This gives you more control over types than with [Any/AnyObject]:
enum Container {
case IntegerValue(Int)
case StringValue(String)
}
var arr: [Container] = [
.IntegerValue(10),
.StringValue("Hello"),
.IntegerValue(42)
]
for item in arr {
switch item {
case .IntegerValue(let val):
println("Integer: \(val)")
case .StringValue(let val):
println("String: \(val)")
}
}
Print
Integer: 10
String: Hello
Integer: 42
Kolja source
share