我遇到以下错误消息:

在没有更多上下文的情况下,表达类型不明确

在这一行:

 Picker(selection: $contact.type, label: Text("Select Testing Type"))


当我在Text中添加了更多行时,错误弹出。有人有同样的问题吗?怎么解决呢?

我正在使用Catalina和iOS11。

这里是整个代码片段:

进口联合收割机
导入SwiftUI

class Contact: ObservableObject {
    var objectWillChange = PassthroughSubject <Void, Never>()
    static let types = ["Functional", "Automation", "Manual", "Mobile App", "Performance", "Security", "E2E", "Integration"]
    static let services = ["HandsOn Testing", "Test Management", "Test Process Setup", "Training", "Test Leadership"]
    var type = 0 {didSet { update()}}
    var service = 0 {didSet { update()}}
    var name = "" {didSet { update()}}
    var company = "" {didSet { update()}}
    var city = "" {didSet { update()}}
    var email = "" {didSet { update()}}

    var isValid: Bool{
        if name.isEmpty || company.isEmpty || city.isEmpty || email.isEmpty {
            return false
        }
    }

    func update(){
        //add log
        objectWillChange.send()
    }
}

struct ContentView: View {
    @ObservedObject var contact = Contact()
    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $contact.type, label: Text("Select Testing Type"))
                    { ForEach(0..<Contact.types.count)
                    {Text(Contact.types[$0]).tag($0)
                        }}

                    Picker(selection: $contact.service, label: Text("Select Testing Service"))
                    {ForEach(0..<Contact.services.count)
                    {Text(Contact.services[$0]).tag($0)
                        }}
                }
                Section {
                    TextField($contact.name, placeholder:Text("Name"))
                    TextField($contact.company, placeholder:Text("Company"))
                    TextField($contact.city, placeholder:Text("City"))
                    TextField($contact.email, placeholder:Text("Email"))

                }

                Section {
                    Button(action: {
                        self.sendForm()
                    }) {
                        Text("Send Form")
                    }
                }
                .navigationBarTitle(Text("Contact us"))

            }

        }}
    func sendForm(){

    }}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

最佳答案

如果您按模块中的原样复制/粘贴代码,那么我认为问题出在


  ForEach(0 ..

您使用类型名称而不是变量名称联系人

10-08 09:24