Swift: Test class type in switch statement

后端 未结 4 386
日久生厌
日久生厌 2020-12-02 04:41

In Swift you can check the class type of an object using \'is\'. How can I incorporate this into a \'switch\' block?

I think it\'s not possible, so I\'m wondering w

4条回答
  •  醉话见心
    2020-12-02 05:09

    Putting up the example for "case is - case is Int, is String:" operation, where multiple cases can be used clubbed together to perform the same activity for Similar Object types. Here "," separating the types in case is operating like a OR operator.

    switch value{
    case is Int, is String:
        if value is Int{
            print("Integer::\(value)")
        }else{
            print("String::\(value)")
        }
    default:
        print("\(value)")
    }
    

    Demo Link

提交回复
热议问题