如何在 SwiftUI 中正确创建按钮。该代码有效,但它在重写它的下一行代码中抛出错误

问题描述 投票:0回答:1

/* 以下代码产生两个警告: 在“try”表达式中没有调用抛出函数 “catch”块无法访问,因为“do”块中没有抛出错误 */

     Button("Delete Account", role: .destructive) {
                    
                            do {
                                try Auth.auth().currentUser?.delete()  
                                userIsLoggedIn = false
                                email = ""
                                password = ""
                            } catch {  
                                // handle error
                            }
                       }
                    }
                } else {
                    content
                        .navigationBarHidden(true)
                        .navigationTitle("ChatMate")
                        .foregroundColor(.blue)
                
            }
        }
    }

//我重写了代码,结果如下:

                        Button("Delete Account", role: .destructive) {
                        if let user = Auth.auth().currentUser {
                            user.delete { error in
                                if let error = error {
                                    // handle error
                                } else {
                                    userIsLoggedIn = false
                                    email = ""
                                    password = ""
                                }
                            }
                        }
                    }

/*但是缺少一个括号或某些东西,因为这会在下一行代码中引发错误: 无法在结果生成器中声明局部计算变量 */

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)
xcode button swiftui
1个回答
0
投票

答案是删掉一个括号,改成

如果让错误=错误{

user.delete { 错误 如果错误 != nil {

© www.soinside.com 2019 - 2024. All rights reserved.