enum method returning a dynamic type

后端 未结 1 659
暗喜
暗喜 2021-01-27 12:50

I have an enum and I\'d like to create a method to return a different type for every case.

For example, I have a dictionary [String: Any]. To process the va

相关标签:
1条回答
  • 2021-01-27 13:32

    I think the core of the problem here is that Swift has strict typing. That means types must be known at compile time. This, obviously, is legal:

    let s : Any = "howdy"
    if let ss = s as? String {
        print(ss)
    }
    

    But this is not legal:

    let s : Any = "howdy"
    let someType = String.self
    if let ss = s as? someType { // *
        print(ss)
    }
    

    someType must be a type; it cannot be a variable hiding a type inside itself. But that is precisely what, in effect, you are asking to do.

    0 讨论(0)
提交回复
热议问题