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

Xcode中使用SwiftUI的Form与Section时出现报错求助

问题

在Xcode中使用SwiftUI的Form和Section组件时遇到报错,即使基础结构也无法正常运行,报错信息如下:

  • Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure
  • Static method 'buildExpression' requires that 'Section' conform to 'View'
  • Trailing closure passed to parameter of type 'Decoder' that does not accept a closure

相关代码:

import SwiftUI
import FirebaseAuth

struct SettingsView: View {
@State private var notificationsEnabled: Bool = false
@State private var locationEnabled: Bool = false
@State private var darkModeEnabled: Bool = false

var body: some View {
    NavigationView {
        
        Form {
            
            // Preferences Section
            Section(header: Text("Preferences")) {
                Toggle("Enable Notifications", isOn: $notificationsEnabled)
                Toggle("Enable Location Services", isOn: $locationEnabled)
                Toggle("Enable Dark Mode", isOn: $darkModeEnabled)
            }

            // Other Settings Section
            Section(header: Text("Terms & Policy")) {
                NavigationLink(destination: PrivacyPolicyView()) {
                    HStack {
                        Text("Privacy Policy")
                        Spacer()
                    }
                }
                NavigationLink(destination: TermsOfServiceView()) {
                    HStack {
                        Text("Terms of Service")
                        Spacer()
                    }
                }
            }
            
            
            // User Account Section
            Section(header: Text("Account")) {
                Button(action: logout) {
                    Text("Logout")
                        .foregroundColor(.red)
                }
                Button(action: deleteAccount) {
                    Text("Delete Account")
                        .foregroundColor(.red)
                }
            }
            
        }
        .navigationTitle("Settings")
    }
}

func logout() {
    // Implement logout functionality here
    do {
        try Auth.auth().signOut()
        print("User logged out")
    } catch let signOutError as NSError {
        print("Error signing out: \(signOutError)")
    }
}

func deleteAccount() {
    // Implement delete account functionality here
    let user = Auth.auth().currentUser

    user?.delete { error in
        if let error = error {
            print("Error deleting account: \(error)")
        } else {
            print("User account deleted")
        }
    }
}
}

struct PrivacyPolicyView: View {
var body: some View {
    Text("Privacy Policy")
        .navigationTitle("Privacy Policy")
}
}

struct TermsOfServiceView: View {
var body: some View {
    Text("Terms of Service")
        .navigationTitle("Terms of Service")
}
}

struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
    SettingsView()
}
}
解决方案

这些报错的核心原因是命名冲突:FirebaseAuth框架中存在与SwiftUI的Form/Section同名的类型,导致编译器无法正确识别SwiftUI的组件,进而抛出类型不匹配的错误。

提供两种修复方案:

方案1:明确指定SwiftUI命名空间

在所有Form和Section前添加SwiftUI.前缀,强制编译器使用SwiftUI的组件类型:

NavigationView {
    SwiftUI.Form {
        // Preferences Section
        SwiftUI.Section(header: Text("Preferences")) {
            Toggle("Enable Notifications", isOn: $notificationsEnabled)
            Toggle("Enable Location Services", isOn: $locationEnabled)
            Toggle("Enable Dark Mode", isOn: $darkModeEnabled)
        }

        // Other Settings Section
        SwiftUI.Section(header: Text("Terms & Policy")) {
            NavigationLink(destination: PrivacyPolicyView()) {
                HStack {
                    Text("Privacy Policy")
                    Spacer()
                }
            }
            NavigationLink(destination: TermsOfServiceView()) {
                HStack {
                    Text("Terms of Service")
                    Spacer()
                }
            }
        }
        
        // User Account Section
        SwiftUI.Section(header: Text("Account")) {
            Button(action: logout) {
                Text("Logout")
                    .foregroundColor(.red)
            }
            Button(action: deleteAccount) {
                Text("Delete Account")
                    .foregroundColor(.red)
            }
        }
    }
    .navigationTitle("Settings")
}

方案2:按需导入FirebaseAuth类型

如果不需要全局导入FirebaseAuth的所有类型,可修改导入语句,仅导入所需的Auth类型,避免引入冲突的命名:

import SwiftUI
import FirebaseAuth.Auth // 仅导入Auth类型,排除其他可能冲突的类型

两种方案任选其一,修改后重新编译即可解决报错,代码可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 11:40:05