首页 > 解决方案 > SwiftUI 选择器 调用中位置 #1、#2、#3 的额外参数

问题描述

我正在尝试使用 swiftUI 制作分段选择器。Xcode一直告诉我代码中有错误,特别是以下2个错误:

  1. 调用中位置#1、#2、#3 的额外参数
  2. “Picker”类型的值没有成员“pickerStyle”

我在一个新的干净项目中运行了这段代码,它编译没有问题,我得到了分段选择器的预期结果。

其他 swiftUI 代码可以正常工作并正确编译。此外,部署目标是 iOS 13.0。

我不确定为什么会发生此错误。

编辑 -没有其他称为 Picker 的结构或类。

编辑 -运行 Xcode 12.3。

import SwiftUI

struct PickerView: View {
    
    @State private var selectedView = 0
    
    var body: some View {
        
        Picker(selection: selectedView, label: Text("What is your favorite color?")) { //error 1 is on this line
            Text("Red").tag(0)
            Text("Green").tag(1)
            Text("Blue").tag(2)
        }.pickerStyle(SegmentedPickerStyle()) //error 2 is on this line
        
    }
}

代码错误

标签: swiftswiftui

解决方案


这是固定的变体。使用 Xcode 12.4 测试。

演示

struct PickerView: View {
    
    @State private var selectedView = 0
    
    var body: some View {
        
        Picker(selection: $selectedView, label: Text("What is your favorite color?")) { //error 1 is on this line
            Text("Red").tag(0)
            Text("Green").tag(1)
            Text("Blue").tag(2)
        }.pickerStyle(SegmentedPickerStyle())
        
    }
}

推荐阅读