There is a protocol:
protocol Valuable {
func value() -> Int
}
and a class which implements the protocol:
class Value: Valuable {
private let v: Int
init(value: Int) {
v = value
}
func value() -> Int {
return v
}
}
There is an array of Value objects stored in a variable of Any type:
let any: Any = [Value(value: 1), Value(value: 2), Value(value: 3)]
It is possible to cast Any to [Value]:
let arrayOfValue = any as? [Value] // [1, 2, 3]
Why it is not possible to case Any to [Valuable]?
let arrayOfValuable = any as! [Valuable] // compiler error BAD INSTRUCTION
let arrayOfValuable2 = any as? [Valuable] // nil