问题复现
在学习Picker选择器时,发现以下报错:
Argument passed to call that takes no arguments
Remove '("Pick a number", selection: $selectedNumber) {
ForEach(1..<101) { number in
Text("\(number)")
}
}'
报错代码:
import SwiftUI
struct Picker: View {
@State private var selectedNumber = 1
var body: some View {
Picker("Pick a number", selection: $selectedNumber) {
ForEach(1..<101) { number in
Text("\(number)")
}
}
.pickerStyle(WheelPickerStyle())
}
}
#Preview {
Picker()
}
经排查发现,问题为SwiftUI视图结构存在命名冲突,即定义的struct Picker与Swift UI自带的Picker控件同名,导致Swift认为我在调取Picker结构,而不是Swift的Picker组件。
解决方案
修改Picker机构的名称,改为SomePicker。