我有一個枚舉案例的數組,我想在這些案件上運行一個switch語句,但得到這個錯誤:在[Directions]類型中找不到'Enum case'North'。開關語句枚舉案例數組
enum Directions {
case north
case west
case east
case south
static let all = [north, west, east, south]
}
class MyViewController {
var directions = Directions.all
func foo() {
switch directions {
case .north: // Error here ('Enum case 'North' not found in type '[Directions]')
print("Going north")
}
}
}
爲什麼你要在switch語句中使用你的枚舉數組? –
你試圖做的是毫無意義的。您需要在陣列中的每個項目上執行開關,而不是整個陣列。例如,你可以問這個問題[北,西,東,南]是否等於北? –
這個問題更多的是關於使用枚舉數組,然後我使用它們。我會在我的函數中檢查一個具體的值。所以會有一個ex: let goingInDirection = .north 然後func foo(goingInDirection){ switch ... } – andromedainiative