Is that possible to do dynamic casting?

My code:

import Foundation


let array = [1,2,3,4,5]

let dictionary:[String:Any] = ["numbers":array]


if let value = dictionary["numbers"] {

    let type = type(of: value)

    print(type) // Array<Int>

    let numbers = value as! Array<Int> // [1, 2, 3, 4, 5]

    //let numbers2 = value as! type // error: use of undeclared type 'type'

}

As you can see, the dynamic casting leads an error. Is there a way to do
this? Thanks.


Zhaoxin
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to