错误信息:”传递给不接受闭包的类型 ‘FormStyleConfiguration’ 的尾随闭包”

huangapple go评论49阅读模式
英文:

error "Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure"

问题

以下是代码的翻译部分,不包括您的问题:

import SwiftUI
import UIKit

struct RegisterParams: View {
    
    @State private var toggle = false
    @State private var showAlert = false
    let userCredentials: UserCredentials

    var alert: Alert {
        Alert(title: Text("无效的电子邮件或密码"), dismissButton: .default(Text("确定"))
    }
    var body: some View {
        VStack{
            Group{
                Form{
                    Label("你的电子邮件是什么", systemImage: "at")
                        .bold()
                    TextField("电子邮件", text: userCredentials.email)
                    Label("输入新密码", systemImage: "lock")
                        .bold()
                    SecureField("密码", text: userCredentials.password)
                    Label("告诉我们你的昵称", systemImage: "person")
                        .bold()
                    TextField("昵称", text: userCredentials.nickname)
                        .bold()
                }
                .scrollContentBackground(.hidden)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .disableAutocorrection(true)
                .textInputAutocapitalization(.never)
                .frame(width: 400, height: 300)
    
                Button ("注册") {
                    toggle.toggle()
                    
                    if (isValidEmailAddr(strToValidate: userCredentials.email) && isValidPassword(strToVailidate: userCredentials.password)) {
                        
                        print("电子邮件和密码有效")
                    }
                    else {
                        self.showAlert.toggle()
                    }
                }
                .alert(isPresented: $showAlert, content: { self.alert })
                .font(.title)
                .buttonStyle(.bordered)
                .frame(width:200, height: 100)
            }.frame(maxHeight: .infinity, alignment: .bottom)
        }.background(.gray)
    }
}

struct RegisterParams_Previews: PreviewProvider {
    static var previews: some View {
        RegisterParams(userCredentials: UserCredentials.init(email: "", password: "", nickname: ""))
    }
}

希望这个翻译对您有帮助。如果您有其他问题,请随时提出。

英文:

import SwiftUI
import UIKit


struct RegisterParams: View {
    
    @State private var toggle = false
    @State private var showAlert = false
    let userCredentials: UserCredentials

    var alert: Alert {
        Alert(title: Text("invalid email or password"), dismissButton: .default(Text("OK")))
    }
    var body: some View {
        VStack{
            Group{
                Form{ //Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure
                    Label("What's your email", systemImage: "at")
                        .bold()
                    TextField("email", text: userCredentials.email)
                    Label("Enter a new password", systemImage: "lock")
                        .bold()
                    SecureField("password", text: userCredentials.password)
                    Label("Tell us your nickname", systemImage: "person")
                        .bold()
                    TextField("nickname", text: userCredentials.nickname)
                        .bold()
                }
                .scrollContentBackground(.hidden)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .disableAutocorrection(true)
                .textInputAutocapitalization(.never)
                .frame(width: 400, height: 300)
    
                Button ("Sign Up") {
                    toggle.toggle()
                    
                    if (isValidEmailAddr(strToValidate: userCredentials.email) && isValidPassword(strToVailidate: userCredentials.password)) {
                        
                        print("Email and Passowrd are valid")
                    }
                    else {
                        self.showAlert.toggle()
                    }
                }
                .alert(isPresented: $showAlert, content: { self.alert })
                .font(.title)
                .buttonStyle(.bordered)
                .frame(width:200, height: 100)
            }.frame(maxHeight: .infinity, alignment: .bottom)
        }.background(.gray)
    }
}
struct RegisterParams_Previews: PreviewProvider {
    static var previews: some View {
        RegisterParams(userCredentials: UserCredentials.init(email: "", password: "", nickname: ""))
    }
}

This is a registerview, the userCredentials constant calls a struct where I stored 3 strings(email, password, nickname)
I tried checking the formatting of the code, and if the brackets were correctly used, but I didn't find any issue.
I think it has something to do with the userCredentials because it's not a @State private var but I am not entirely sure about that.

答案1

得分: 1

是的,你的直觉是正确的,userCredentials 需要使用 @State 进行包装,因为你修改了它的属性:

@State var userCredentials: UserCredentials

然后,你需要为每个属性传递一个绑定,以便它们可以被更改:

TextField("email", text: $userCredentials.email)
// 对其他属性也是一样的。

我不知道 UserCredentials 是什么,但在这里你正在编辑的属性也需要声明为 var

英文:

Yes your hunch is correct, userCredentials needs to be wrapped with @State since you modify its properties

@State var userCredentials: UserCredentials

and then you need to pass a binding to each property where they can be changed

TextField("email", text: $userCredentials.email)
// same for the other properties.

I don't know what UserCredentials is but the properties you are editing here needs to be var declared as well.

huangapple
  • 本文由 发表于 2023年2月14日 22:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定