/* 以下代码产生两个警告: 在“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)
答案是删掉一个括号,改成
如果让错误=错误{
到
user.delete { 错误 如果错误 != nil {