小于或大于Swift switch语句
我所熟悉的switch在斯威夫特的语句,但不知道如何使用替代这段代码switch : 
if someVar < 0 {
    // do something
} else if someVar == 0 {
    // do something else
} else if someVar > 0 {
    // etc
}
  这是一种方法。  假设someVar是一个Int或其他Comparable ,您可以选择将操作数分配给一个新变量。  这让你可以使用where关键字来确定它的范围: 
var someVar = 3
switch someVar {
case let x where x < 0:
    print("x is (x)")
case let x where x == 0:
    print("x is (x)")
case let x where x > 0:
    print("x is (x)")
default:
    print("this is impossible")
}
这可以简化一点:
switch someVar {
case _ where someVar < 0:
    print("someVar is (someVar)")
case 0:
    print("someVar is 0")
case _ where someVar > 0:
    print("someVar is (someVar)")
default:
    print("this is impossible")
}
  您还可以完全避免使用范围匹配的where关键字: 
switch someVar {
case Int.min..<0:
    print("someVar is (someVar)")
case 0:
    print("someVar is 0")
default:
    print("someVar is (someVar)")
}
使用Swift 4,您可以选择以下其中一个开关来替换if语句。
  #1使用开关与CountablePartialRangeFrom和PartialRangeUpTo 
let value = 1
switch value {
case 1...:
    print("greater than zero")
case 0:
    print("zero")
case ..<0:
    print("less than zero")
default:
    fatalError()
}
  #2使用开关与CountableClosedRange , CountableRange , Int的max静态属性和Int的min静态属性 
let value = 1
switch value {
case 1 ... Int.max:
    print("greater than zero")
case Int.min ..< 0:
    print("less than zero")
case 0:
    print("zero")
default:
    fatalError()
}
#3在where子句中使用开关
let value = 1
switch value {
case let val where val > 0:
    print("(val) is greater than zero")
case let val where val == 0:
    print("(val) is zero")
case let val where val < 0:
    print("(val) is less than zero")
default:
    fatalError()
}
  #4将where子句和赋值分配给_ 
let value = 1
switch value {
case _ where value > 0:
    print("greater than zero")
case _ where value == 0:
    print("zero")
case _ where value < 0:
    print("less than zero")
default:
    fatalError()
}
  #5使用带RangeExpression协议的开关~=(_:_:)运算符 
let value = 1
switch true {
case 1... ~= value:
    print("greater than zero")
case ..<0 ~= value:
    print("less than zero")
default:
    print("zero")
}
  #6使用带有Equatable协议的开关~=(_:_:)运算符 
let value = 1
switch true {
case value > 0:
    print("greater than zero")
case value < 0:
    print("less than zero")
case 0 ~= value:
    print("zero")
default:
    fatalError()
}
  #7使用开关与CountablePartialRangeFrom , PartialRangeUpTo和RangeExpression的contains(_:)方法 
let value = 1
switch true {
case (1...).contains(value):
    print("greater than zero")
case (..<0).contains(value):
    print("less than zero")
default:
    print("zero")
}
  引擎盖下的switch语句使用~=运算符。  所以这: 
let x = 2
switch x {
case 1: print(1)
case 2: print(2)
case 3..<5: print(3..<5)
default: break
}
对此的Desugars:
if 1          ~= x { print(1) }
else if 2     ~= x { print(2) }
else if 3..<5 ~= x { print(3..<5) }
else {  }
  如果你看一下标准的库引用,它可以准确地告诉你~=是否被超载了:include包含范围匹配,并且等同于可比较的东西。  (不包括enum-case匹配,这是一种语言功能,而不是std lib中的函数) 
你会发现它与左侧的直布尔值不匹配。 对于那些比较,你需要添加一个where语句。
  除非......你自己重载了~=运算符。  (这通常不被推荐)一种可能性是这样的: 
func ~= <T> (lhs: T -> Bool, rhs: T) -> Bool {
  return lhs(rhs)
}
这样匹配一个函数,该函数将左边的布尔值返回给它右边的参数。 以下是你可以使用它的一些东西:
func isEven(n: Int) -> Bool { return n % 2 == 0 }
switch 2 {
case isEven: print("Even!")
default:     print("Odd!")
}
对于你的情况,你可能会有一个如下所示的陈述:
switch someVar {
case isNegative: ...
case 0: ...
case isPositive: ...
}
  但是现在你必须定义新的isNegative和isPositive函数。  除非你让更多的操作员重载...... 
您可以将正常的中缀运算符重载为curried前缀或后缀运算符。 这是一个例子:
postfix operator < {}
postfix func < <T : Comparable>(lhs: T)(_ rhs: T) -> Bool {
  return lhs < rhs
}
这会像这样工作:
let isGreaterThanFive = 5<
isGreaterThanFive(6) // true
isGreaterThanFive(5) // false
将它与前面的函数结合起来,你的switch语句可以像这样:
switch someVar {
case 0< : print("Bigger than 0")
case 0  : print("0")
default : print("Less than 0")
}
  现在,你可能不应该在实践中使用这种类型的东西:它有点狡猾。  你(可能)最好坚持使用where语句。  这就是说,开关语句的模式 
switch x {
case negative:
case 0:
case positive:
}
要么
switch x {
case lessThan(someNumber):
case someNumber:
case greaterThan(someNumber):
}
似乎很普遍,值得考虑。
链接地址: http://www.djcxy.com/p/58541.html上一篇: Lesser than or greater than in Swift switch statement
下一篇: Why does Java switch on contiguous ints appear to run faster with added cases?
