如何为数组进行切换?
How to make a switch for an array?
这是我的代码:
var animalArray = ["cow","pig"]
switch animalArray {
case ["cow","pig"],["pig","cow"]:
println("You Win!")
default:
println("Keep Trying")
我收到错误:"Type 'Array' does not conform to protocol 'IntervalType'" 行 "case ["cow","pig"],["pig","cow"]:"。我究竟做错了什么?
你不能用数组来做到这一点。但是您可以使用 contains()
方法检查它并遍历要测试的数组(此处 secondArray
):
var animalArray:[String] = ["cow", "pig"]
var secondArray:[String] = ["cow", "test"]
for s in secondArray{
if(contains(animalArray, s)){
println("animalArray Contains \(s)")
}
}
switch
语句需要 Int
。想想这个:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection: Int = animalDict["cow"]!
switch animalSelection {
case 0:
println("The Cow Wins!")
case 1:
println("The Pig Wins!")
default:
println("Keep Trying")
}
//prints "The Cow Wins!"
编辑 1:
感谢大家的评论。我认为这是更健壮的代码:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection: Int? = animalDict["horse"]
if animalSelection as Int? != nil {
switch animalSelection! {
case 0:
println("The Cow Wins!")
case 1:
println("The Pig Wins!")
default:
println("Keep Trying")
}
} else {
println("Keep Trying")
}
//prints "Keep Trying"
如果我说:
,它仍然会打印 The Cow Wins
var animalSelection:Int? = animalDict["cow"]
编辑 2:
根据@AirSpeedVelocity 的评论,我测试了以下代码。比我自己的代码优雅多了:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection = animalDict["horse"]
switch animalSelection {
case .Some(0):
println("The Cow Wins!")
case .Some(1):
println("The Pig Wins!")
case .None:
println("Not a valid Selection")
default:
println("Keep Trying")
}
如果您想比较两个数组而不考虑它们的条目顺序,那么我建议如下:
var referenceAnimal = ["cow", "pig"]
var animalsToTest = ["pig", "cow"]
sort(&referenceAnimal)
sort(&animalsToTest)
if referenceAnimal == animalsToTest {
println("You Win!")
} else {
println("Keep Trying")
}
您可以通过适当重载 ~=
使 switch
能够匹配数组:
func ~=<T: Equatable>(lhs: [T], rhs: [T]) -> Bool {
return lhs == rhs
}
var animalArray = ["cow","pig"]
switch animalArray {
case ["cow","pig"],["pig","cow"]:
println("You Win!”) // this will now match
default:
println("Keep Trying")
}
尽管这是否是个好主意值得怀疑。
这与@Christian Woerz 的回答几乎相同,但在这些情况下我是 reduce
的傻瓜。
var animalArray = ["cow","pig"]
var answerArray = ["pig","cow"]
let isCorrect = answerArray.reduce(true) { bool, animal in
return bool && contains(animalArray, animal)
}
如果顺序不重要,您可以这样做。
import Foundation
let animalArray = ["cow","pig"]
extension Array where Element == String {
static func ~=(pattern: Array, value: Array) -> Bool {
return pattern == value
}
}
switch animalArray {
case ["cow", "pig"], ["pig","cow"]:
print("You Win!")
default:
print("Keep Trying")
}
请记住,这会将它应用到所有 Array<String>
,您可能想将它们包装起来,或者换一种方式将它应用到所有 Equatable
。
为了将来参考,将开关包装在 for-in 循环中是另一种选择。
var animalArray = ["cow", "pig"]
for animal in animalArray {
switch animal {
case "cow":
print("win")
case "pig":
print("lose")
default:
print("try again")
}
}
这是我的代码:
var animalArray = ["cow","pig"]
switch animalArray {
case ["cow","pig"],["pig","cow"]:
println("You Win!")
default:
println("Keep Trying")
我收到错误:"Type 'Array' does not conform to protocol 'IntervalType'" 行 "case ["cow","pig"],["pig","cow"]:"。我究竟做错了什么?
你不能用数组来做到这一点。但是您可以使用 contains()
方法检查它并遍历要测试的数组(此处 secondArray
):
var animalArray:[String] = ["cow", "pig"]
var secondArray:[String] = ["cow", "test"]
for s in secondArray{
if(contains(animalArray, s)){
println("animalArray Contains \(s)")
}
}
switch
语句需要 Int
。想想这个:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection: Int = animalDict["cow"]!
switch animalSelection {
case 0:
println("The Cow Wins!")
case 1:
println("The Pig Wins!")
default:
println("Keep Trying")
}
//prints "The Cow Wins!"
编辑 1:
感谢大家的评论。我认为这是更健壮的代码:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection: Int? = animalDict["horse"]
if animalSelection as Int? != nil {
switch animalSelection! {
case 0:
println("The Cow Wins!")
case 1:
println("The Pig Wins!")
default:
println("Keep Trying")
}
} else {
println("Keep Trying")
}
//prints "Keep Trying"
如果我说:
,它仍然会打印The Cow Wins
var animalSelection:Int? = animalDict["cow"]
编辑 2:
根据@AirSpeedVelocity 的评论,我测试了以下代码。比我自己的代码优雅多了:
var animalDict: [String: Int] = ["cow": 0,"pig": 1]
var animalSelection = animalDict["horse"]
switch animalSelection {
case .Some(0):
println("The Cow Wins!")
case .Some(1):
println("The Pig Wins!")
case .None:
println("Not a valid Selection")
default:
println("Keep Trying")
}
如果您想比较两个数组而不考虑它们的条目顺序,那么我建议如下:
var referenceAnimal = ["cow", "pig"]
var animalsToTest = ["pig", "cow"]
sort(&referenceAnimal)
sort(&animalsToTest)
if referenceAnimal == animalsToTest {
println("You Win!")
} else {
println("Keep Trying")
}
您可以通过适当重载 ~=
使 switch
能够匹配数组:
func ~=<T: Equatable>(lhs: [T], rhs: [T]) -> Bool {
return lhs == rhs
}
var animalArray = ["cow","pig"]
switch animalArray {
case ["cow","pig"],["pig","cow"]:
println("You Win!”) // this will now match
default:
println("Keep Trying")
}
尽管这是否是个好主意值得怀疑。
这与@Christian Woerz 的回答几乎相同,但在这些情况下我是 reduce
的傻瓜。
var animalArray = ["cow","pig"]
var answerArray = ["pig","cow"]
let isCorrect = answerArray.reduce(true) { bool, animal in
return bool && contains(animalArray, animal)
}
如果顺序不重要,您可以这样做。
import Foundation
let animalArray = ["cow","pig"]
extension Array where Element == String {
static func ~=(pattern: Array, value: Array) -> Bool {
return pattern == value
}
}
switch animalArray {
case ["cow", "pig"], ["pig","cow"]:
print("You Win!")
default:
print("Keep Trying")
}
请记住,这会将它应用到所有 Array<String>
,您可能想将它们包装起来,或者换一种方式将它应用到所有 Equatable
。
为了将来参考,将开关包装在 for-in 循环中是另一种选择。
var animalArray = ["cow", "pig"]
for animal in animalArray {
switch animal {
case "cow":
print("win")
case "pig":
print("lose")
default:
print("try again")
}
}