【问题标题】:Swift: Test class type in switch statementSwift:在 switch 语句中测试类类型
【发布时间】:2014-11-01 16:31:36
【问题描述】:

在 Swift 中,您可以使用“is”来检查对象的类类型。如何将其合并到“开关”块中?

我认为这是不可能的,所以我想知道解决这个问题的最佳方法是什么。

【问题讨论】:

    标签: class swift switch-statement


    【解决方案1】:

    如果您没有值,则可以是任何对象:

    斯威夫特 4

    func test(_ val:Any) {
        switch val {
        case is NSString:
            print("it is NSString")
        case is String:
            print("it is a String")
        case is Int:
            print("it is int")
        default:
            print(val)
        }
    }
    
    
    let str: NSString = "some nsstring value"
    let i:Int=1
    test(str) 
    // it is NSString
    test(i) 
    // it is int
    

    【讨论】:

      【解决方案2】:

      我喜欢这种语法:

      switch thing {
      case _ as Int: print("thing is Int")
      case _ as Double: print("thing is Double")
      }
      

      因为它使您可以快速扩展功能,如下所示:

      switch thing {
      case let myInt as Int: print("\(myInt) is Int")
      case _ as Double: print("thing is Double")
      }
      

      【讨论】:

      • 我更喜欢as,因为它也会转换类型。
      【解决方案3】:

      您绝对可以在switch 块中使用is。请参阅 Swift 编程语言中的“Any 和 AnyObject 的类型转换”(当然不限于 Any)。他们有一个广泛的例子:

      for thing in things {
          switch thing {
          case 0 as Int:
              println("zero as an Int")
          case 0 as Double:
              println("zero as a Double")
          case let someInt as Int:
              println("an integer value of \(someInt)")
          case let someDouble as Double where someDouble > 0:
              println("a positive double value of \(someDouble)")
      // here it comes:
          case is Double:
              println("some other double value that I don't want to print")
          case let someString as String:
              println("a string value of \"\(someString)\"")
          case let (x, y) as (Double, Double):
              println("an (x, y) point at \(x), \(y)")
          case let movie as Movie:
              println("a movie called '\(movie.name)', dir. \(movie.director)")
          default:
              println("something else")
          }
      }
      

      【讨论】:

      • 嗨,罗伯。只是好奇:因为我们在上面的任何cases 中都没有在 switch` 中使用thing,所以在这里使用thing 有什么用?我没看到。谢谢。
      • thing 是在每种情况下测试的值。所以如果事物是​​一个电影,它的值将被绑定到符号电影。
      • 在第 n 次谷歌搜索后看到这个已经被投票和加星标的答案感觉就像回家一样。
      【解决方案4】:

      举出“case is - case is Int, is String:”操作的示例,其中多个 case 可以组合使用以针对相似对象类型执行相同的活动。这里 "," 分隔类型以防万一像 OR 运算符一样操作。

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

      Demo Link

      【讨论】:

      • 将两个案例放在一起只是为了通过if 将它们分开可能不是证明您观点的最佳示例。
      • 如果value 可以是IntFloatDouble 之一,并且以相同的方式处理FloatDouble,那可能会更好。
      猜你喜欢
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多