You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中Form闭包传参错误及切换List后的问题求助

SwiftUI开发求助:Form闭包错误及List替换后的新问题

我在SwiftUI开发中遇到无法解决的问题:

  • 代码中Form {行触发错误:「Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure」
  • 尝试将Form替换为List后,出现3个新错误
  • 已尝试多种解决方法及AI工具均无效,无法在此停滞,急需帮助推进开发

相关代码

DetailsView代码

import SwiftUI

struct DetailsView: View {
    @StateObject private var authViewModel: AuthViewModel = AuthViewModel()
    
    @State private var ageText: String = ""
    @State private var selectedCountryIndex = 0
    
        let countries = [
            "Global"
        ]
    
    var body: some View {
        ZStack {
            Color("BackgroundColor")
                .ignoresSafeArea()
            
            VStack {
                Form {
                    Section("User Details") {
                        UserDetailRowView(labelText: "Username", text: $authViewModel.currentUser?.username ?? "")
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                        
                        UserDetailRowView(labelText: "First Name", text: $authViewModel.currentUser?.firstName ?? "")
                            .textFieldStyle(RoundedBorderTextFieldStyle())

                        UserDetailRowView(labelText: "Last Name", text: $authViewModel.currentUser?.lastName ?? "")
                            .textFieldStyle(RoundedBorderTextFieldStyle())

                    }
                    
                    Section("Additional Information") {
                        HStack {
                            Text("Age:")
                                .font(.headline)
                            TextField("Enter age", text: $ageText)
                                .keyboardType(.numberPad)
                                .textFieldStyle(RoundedBorderTextFieldStyle())
                        }
                        
                        Picker("Country", selection: $selectedCountryIndex) {
                            ForEach(0..<countries.count, id: \.self) { index in
                                Text(countries[index])
                            }
                        }
                        .pickerStyle(MenuPickerStyle())
                    }
                }
                .listStyle(GroupedListStyle())
                
                Button {
                    // Add code to update the user details
                } label: {
                    Text("Save")
                        .foregroundColor(Color("SecondaryColor"))
                        .padding()
                        .background(Color("HighlightColor"))
                        .cornerRadius(10)
                }
            }
        }
        .navigationBarTitle("Edit Profile")
    }
}

struct DetailsView_Previews: PreviewProvider {
    static var previews: some View {
        DetailsView()
            .environmentObject(AuthViewModel())
    }
}

其他结构体代码

struct UserDetailRowView: View {
    let labelText: String
    let text: Binding<String>
    
    var body: some View {
        VStack(alignment: .leading) {
            Text(labelText)
                .font(.headline)
                .foregroundColor(Color("TertiaryColor"))
            
            TextField("Enter \(labelText)", text: text)
                .textFieldStyle(RoundedBorderTextFieldStyle())
        }
        .padding(.vertical, 8)
    }
}

struct User: Identifiable, Codable {
    let id: String
    let fullName: String
    let email: String
    var firstName: String
    var lastName: String
    var age: Int
    
    var initials: String {
        let formatter = PersonNameComponentsFormatter()
        if let components = formatter.personNameComponents(from: fullName) {
            formatter.style = .abbreviated
            return formatter.string(from: components)
        }
        
        return ""
    }
}

extension User {
    static var MOCK_USER = User(id: NSUUID().uuidString, fullName: "John Smith", email: "test@example.com", firstName: "Tester", lastName: "Acc", age: 16)
}

AuthViewModel类初始代码

@MainActor
class AuthViewModel: ObservableObject {
    @Published var userSession: FirebaseAuth.User?
    @Published var currentUser: User?
    
    // ...
}

内容的提问来源于stack exchange,提问作者Mohamed Badir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:02:02