[swift-users] Can I do dynamic casting?

Zhao Xin owenzx at gmail.com
Thu Aug 25 03:26:27 CDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160825/03e72eba/attachment.html>


More information about the swift-users mailing list