小于或大于 Swift switch 语句
Lesser than or greater than in Swift switch statement
我熟悉 Swift 中的 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)")
}
由于有人已经发布了 case let x where x < 0:
,这里有一个替代方案,其中 someVar
是 Int
。
switch someVar{
case Int.min...0: // do something
case 0: // do something
default: // do something
}
这是 someVar
是 Double
的替代方法:
case -(Double.infinity)...0: // do something
// etc
switch
语句在底层使用了 ~=
运算符。所以这个:
let x = 2
switch x {
case 1: print(1)
case 2: print(2)
case 3..<5: print(3..<5)
default: break
}
脱糖:
if 1 ~= x { print(1) }
else if 2 ~= x { print(2) }
else if 3..<5 ~= x { print(3..<5) }
else { }
If you look at the standard library reference, it can tell you exactly what the ~=
is overloaded to do:包括范围匹配,等同于可等同的东西。 (不包括枚举大小写匹配,这是一种语言特性,而不是标准库中的函数)
您会发现它与左侧的直接布尔值不匹配。对于这些类型的比较,您需要添加一个 where 语句。
除非...您自己重载 ~=
运算符。 (通常不推荐)一种可能是这样的:
func ~= <T> (lhs: T -> Bool, rhs: T) -> Bool {
return lhs(rhs)
}
所以匹配一个函数,returns 左边的布尔值和右边的参数。您可以将其用于以下用途:
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
函数。除非你重载一些运算符...
您可以将普通中缀运算符重载为柯里化前缀或后缀运算符。这是一个例子:
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 语句模式
switch x {
case negative:
case 0:
case positive:
}
或
switch x {
case lessThan(someNumber):
case someNumber:
case greaterThan(someNumber):
}
看起来很常见,值得考虑。
您可以:
switch true {
case someVar < 0:
print("less than zero")
case someVar == 0:
print("eq 0")
default:
print("otherwise")
}
这是范围的样子
switch average {
case 0..<40: //greater or equal than 0 and less than 40
return "T"
case 40..<55: //greater or equal than 40 and less than 55
return "D"
case 55..<70: //greater or equal than 55 and less than 70
return "P"
case 70..<80: //greater or equal than 70 and less than 80
return "A"
case 80..<90: //greater or equal than 80 and less than 90
return "E"
case 90...100: //greater or equal than 90 and less or equal than 100
return "O"
default:
return "Z"
}
<0
表达式不起作用(不再?)所以我得到了这个:
Swift 3.0:
switch someVar {
case 0:
// it's zero
case 0 ..< .greatestFiniteMagnitude:
// it's greater than zero
default:
// it's less than zero
}
使用 Swift 5,您可以选择以下开关之一来替换您的 if 语句。
#1 将 switch 与 PartialRangeFrom
和 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 将 switch 与 ClosedRange
和 Range
一起使用
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 子句中使用 switch
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 将 switch 与 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 将 switch 与 RangeExpression
协议的 ~=(_:_:)
运算符
一起使用
let value = 1
switch true {
case 1... ~= value:
print("greater than zero")
case ..<0 ~= value:
print("less than zero")
default:
print("zero")
}
#6 将 switch 与 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 将 switch 与 PartialRangeFrom
、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")
}
很高兴 Swift 4 解决了问题:
作为 3 中的解决方法,我做了:
switch translation.x {
case 0..<200:
print(translation.x, slideLimit)
case -200..<0:
print(translation.x, slideLimit)
default:
break
}
有效但不理想
Swift 5 现在很干净
switch array.count {
case 3..<.max:
print("Array is greater than or equal to 3")
case .min..<3:
print("Array is less than 3")
default:
break
}
我能想出的最干净的解决方案:
switch someVar {
case ..<0:
// do something
case 0:
// do something else
default:
// etc
}
我熟悉 Swift 中的 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)")
}
由于有人已经发布了 case let x where x < 0:
,这里有一个替代方案,其中 someVar
是 Int
。
switch someVar{
case Int.min...0: // do something
case 0: // do something
default: // do something
}
这是 someVar
是 Double
的替代方法:
case -(Double.infinity)...0: // do something
// etc
switch
语句在底层使用了 ~=
运算符。所以这个:
let x = 2
switch x {
case 1: print(1)
case 2: print(2)
case 3..<5: print(3..<5)
default: break
}
脱糖:
if 1 ~= x { print(1) }
else if 2 ~= x { print(2) }
else if 3..<5 ~= x { print(3..<5) }
else { }
If you look at the standard library reference, it can tell you exactly what the ~=
is overloaded to do:包括范围匹配,等同于可等同的东西。 (不包括枚举大小写匹配,这是一种语言特性,而不是标准库中的函数)
您会发现它与左侧的直接布尔值不匹配。对于这些类型的比较,您需要添加一个 where 语句。
除非...您自己重载 ~=
运算符。 (通常不推荐)一种可能是这样的:
func ~= <T> (lhs: T -> Bool, rhs: T) -> Bool {
return lhs(rhs)
}
所以匹配一个函数,returns 左边的布尔值和右边的参数。您可以将其用于以下用途:
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
函数。除非你重载一些运算符...
您可以将普通中缀运算符重载为柯里化前缀或后缀运算符。这是一个例子:
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):
}
看起来很常见,值得考虑。
您可以:
switch true {
case someVar < 0:
print("less than zero")
case someVar == 0:
print("eq 0")
default:
print("otherwise")
}
这是范围的样子
switch average {
case 0..<40: //greater or equal than 0 and less than 40
return "T"
case 40..<55: //greater or equal than 40 and less than 55
return "D"
case 55..<70: //greater or equal than 55 and less than 70
return "P"
case 70..<80: //greater or equal than 70 and less than 80
return "A"
case 80..<90: //greater or equal than 80 and less than 90
return "E"
case 90...100: //greater or equal than 90 and less or equal than 100
return "O"
default:
return "Z"
}
<0
表达式不起作用(不再?)所以我得到了这个:
Swift 3.0:
switch someVar {
case 0:
// it's zero
case 0 ..< .greatestFiniteMagnitude:
// it's greater than zero
default:
// it's less than zero
}
使用 Swift 5,您可以选择以下开关之一来替换您的 if 语句。
#1 将 switch 与 PartialRangeFrom
和 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 将 switch 与 ClosedRange
和 Range
一起使用
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 子句中使用 switch
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 将 switch 与 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 将 switch 与 RangeExpression
协议的 ~=(_:_:)
运算符
一起使用
let value = 1
switch true {
case 1... ~= value:
print("greater than zero")
case ..<0 ~= value:
print("less than zero")
default:
print("zero")
}
#6 将 switch 与 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 将 switch 与 PartialRangeFrom
、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")
}
很高兴 Swift 4 解决了问题:
作为 3 中的解决方法,我做了:
switch translation.x {
case 0..<200:
print(translation.x, slideLimit)
case -200..<0:
print(translation.x, slideLimit)
default:
break
}
有效但不理想
Swift 5 现在很干净
switch array.count {
case 3..<.max:
print("Array is greater than or equal to 3")
case .min..<3:
print("Array is less than 3")
default:
break
}
我能想出的最干净的解决方案:
switch someVar {
case ..<0:
// do something
case 0:
// do something else
default:
// etc
}