Match the data type of a object in Swift

前端 未结 2 1916
终归单人心
终归单人心 2021-01-18 17:17

How to match the data type of a object in Swift?

Like:

var xyz : Any
    xyz = 1;
    switch xyz
 {
    case let x where xyz as?AnyObject[]:
                 


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 18:02

    Putting up an interesting usage of "case is" i.e., "case is Int, is String", "," behavior is like OR operator.

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

    Similar Post with demo link

提交回复
热议问题