我尝试了以下代码,我可以将Array转换为类型参数。
let x: Array = [21,43,12]
let z: Any = x
if let arr = x as? Array<Any>{
print("yes") // print "yes"
}
但为什么以下代码根本不起作用:
struct Base<T>{
let val: T
init(_ val: T){
self.val = val
}
}
let y = Base(7)
let an: Any = y
if let temp = an as? Base<Any> {
print("yes") // the line won't print.
}
这是一个错误吗?
是否可以这样做:
let arr = [232,32,55]
let x: Any = arr
if x is Array { // error.
print("yes") // won't work. is any way to make it work?
}