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

SwiftUI删除账户按钮代码报错求助:修改后仍存语法错误

SwiftUI 删除账户按钮问题解决方案

问题1:初始代码的警告修复

初始代码的两个警告源于误用了try/catch处理Firebase Auth的异步方法:Firebase的currentUser?.delete()是带闭包的异步方法,并非同步抛出错误的函数,因此try和catch完全多余。

正确异步调用写法

直接使用带completion handler的版本处理结果:

Button("Delete Account", role: .destructive) {
    guard let user = Auth.auth().currentUser else { return }
    user.delete { error in
        if let error = error {
            // 处理删除失败逻辑,例如弹窗提示
            print("删除账户失败:\(error.localizedDescription)")
        } else {
            // 删除成功,重置用户状态
            userIsLoggedIn = false
            email = ""
            password = ""
        }
    }
}

问题2:"Cannot declare local computed variable in result builder"错误修复

这个错误是代码语法不完整或结构错误导致的,需从两方面修正:

步骤1:补全content计算属性代码

你提供的content代码中,ZStack和VStack均未闭合,必须补全所有View的闭包:

var content: some View {
    ZStack {
        Color.blue
            .ignoresSafeArea()
        Circle()
            .scale(1.7)
            .foregroundColor(.white.opacity(0.15))
        Circle()
            .scale(1.35)
            .foregroundColor(.white)
        
        VStack {
            Text("Login")
                .font(.largeTitle)
                .bold()
                .padding()
                .foregroundColor(.green)
            TextField("email", text: $email)
                .padding()
                .frame(width: 300, height: 50)
                .background(Color.black.opacity(0.25))
                .cornerRadius(10)
                .textCase(.lowercase)
                .autocapitalization(.none)
            SecureField("Password", text: $password)
                .padding()
                .frame(width: 300, height: 50)
                .background(Color.black.opacity(0.25))
                .cornerRadius(10)
                .autocapitalization(.none)
            
            // 可在此添加登录按钮等其他控件
        }
    }
}

步骤2:确保按钮所在代码结构合法

假设按钮放在用户已登录的分支中,需保证整个body的语法正确,示例如下:

var body: some View {
    if userIsLoggedIn {
        NavigationStack {
            Text("已登录")
                .navigationTitle("ChatMate")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        // 放置删除账户按钮
                        Button("Delete Account", role: .destructive) {
                            guard let user = Auth.auth().currentUser else { return }
                            user.delete { error in
                                if let error = error {
                                    print("删除失败:\(error)")
                                } else {
                                    userIsLoggedIn = false
                                    email = ""
                                    password = ""
                                }
                            }
                        }
                    }
                }
        }
    } else {
        content
            .navigationBarHidden(true)
            .navigationTitle("ChatMate")
            .foregroundColor(.blue)
    }
}

最终验证要点

  1. 所有View闭包(如ZStack、VStack、if/else)必须正确闭合
  2. 禁止在View的result builder(如body或content的闭包)内声明计算属性
  3. Firebase的delete()方法必须使用异步闭包处理结果,不再使用错误的try/catch

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:05:33