【发布时间】:2020-07-18 07:56:11
【问题描述】:
我正在快速学习,现在我已经插入了 2 个调用结构 Animal 的选择器。 我无法理解的是如何告诉 swift 如果第一个选择器选择了一个枚举值,那么第二个选择器可用的枚举中不能存在相同的值,因为它已经被选择了。
非常感谢:)
import SwiftUI
enum Animal: String, CaseIterable {
case selectCase = "Select"
case bear = "Bear"
case cat = "Cat"
case dog = "Dog"
case lion = "Lion"
case tiger = "Tiger"
static var animals: [String] = [selectCase.rawValue, bear.rawValue, cat.rawValue, dog.rawValue, lion.rawValue, tiger.rawValue]
}
struct ContentView: View {
@State private var Picker1: String = Animal.animals[0]
@State private var Picker2: String = Animal.animals[0]
var body: some View {
NavigationView {
Form {
Section(header: Text("Animals")
.foregroundColor(.black)
.font(.system(size: 15))
.fontWeight(.bold)) {
Picker(selection: $Picker1, label: Text("Select first animal")) {
ForEach(Animal.animals, id: \.self) { element in
Text(element)
}
}
Picker(selection: $Picker2, label: Text("Select second animal")) {
ForEach(Animal.animals, id: \.self) { element2 in
Text(element2)
}
}
}.font(.system(size: 15))
}.navigationBarTitle("List", displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
【问题讨论】: