'How to do hyper-dynamic typecast with "as!" in Swift?

I'm trying to do something like the following to ultimately avoid a long chain of if/else in swift.

Unfortunately, Swift isn't having it, and I'm having a difficult time figuring out how to search for the appropriate lexical representation, if there even is one.

Note: I'm using Any, instead of AnyClass as the value type in the dictionary because for/in doesn't enumerate AnyClass values, and formerly I was using a loop.

    var ids : [ String : Any ] =  [ "someViewControllerIdA"  : SomeViewControllerA.self,
                                    "someViewControllerIdB"  : SomeViewControllerB.self,
                                    "someViewControllerIdC"  : SomeViewControllerC.self]

    let n = 0
    let contentVC = storyboard?.instantiateViewController(withIdentifier: ids.keys[n]) as! ids.values[n]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source