我试图打开我的应用程序的注册屏幕应用程序崩溃,我得到这个错误下面。我甚至不能打开屏幕,所以我不认为电子邮件地址上的点有什么问题。有人能帮我解决这个问题吗?
注意:我是ios开发的初学者,很抱歉我的英语不好。: -)
谢谢。
错误:
(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
我的代码:
struct SignUpView: View {
@State var username: String = ""
@State var email: String = ""
@State var password: String = ""
@State var confrim_password = ""
@State private var showingAlert = false
@State var dialogErrorMsg: String? = ""
@State var singedIn = false
var ref: DatabaseReference! = Database.database().reference()
@EnvironmentObject var viewModel: AppViewModel
func signIn(email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if result != nil, error == nil {
singedIn = true
}else {
dialogErrorMsg = error?.localizedDescription
showingAlert = true
}
}
}
func signUp(username: String, email: String, password: String) {
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if result != nil, error == nil {
let user = ["email": email,
"name": username,
"id": Auth.auth().currentUser?.uid,
"photoUrl": "none",
"premium": "false"]
// Update display name on FirebaseAuth
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = username
changeRequest?.commitChanges()
// Add user to Real-Time DB
self.ref.child(Auth.auth().currentUser?.uid ?? "none").setValue(user)
}else {
showingAlert = true
dialogErrorMsg = error?.localizedDescription
}
}
}
var body: some View {
NavigationView {
ScrollView {
VStack {
Image("logo_transparent").resizable().scaledToFit()
.frame(width: 200, height: 200, alignment: .top)
.padding(.top, -40)
Text("Sign up")
.bold()
.font(.system(size: 40))
TextField("Usename", text: $username)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
TextField("Email", text: $email)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
.autocapitalization(.none)
SecureField("Password", text: $password)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
SecureField("Confirm Password", text: $confrim_password)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
.padding(.bottom, 25)
Text("Sign up")
.bold()
.font(.system(size: 25))
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 50)
.background(Color.blue)
.cornerRadius(40)
.onTapGesture {
if username == "" || email == "" || password == "" || confrim_password == "" {
showingAlert = true
dialogErrorMsg = "Please fill all the values"
}else {
if password != confrim_password {
showingAlert = true
dialogErrorMsg = "Passwords do not match"
}else {
signUp(username: username, email: email, password: password)
signIn(email: email, password: password)
}
}
}.alert(isPresented: $showingAlert, content: {
Alert(title: Text("Error"), message: Text(dialogErrorMsg!), dismissButton: .default(Text("Got it!")))
})
// Navigate to SelectProfileImageView when the user is singed in
NavigationLink("", destination: SelectProfileImageView().navigationTitle("Set Profile Picture").navigationBarTitleDisplayMode(.inline), isActive: $singedIn)
NavigationLink(destination:
LoginView().navigationBarHidden(true)) {
Text("Already have an account? Sing in")
.bold()
.font(.system(size: 20))
.padding(.bottom, 300)
}
.padding(.top, 20)
}.navigationBarHidden(true)
}.navigationBarHidden(true)
}
.edgesIgnoringSafeArea(.all)
.navigationBarHidden(true)
.navigationTitle("")
}
}
struct SignUpView_Previews: PreviewProvider {
static var previews: some View {
SignUpView()
}
}
我猜这里错误发生在:
// Add user to Real-Time DB
self.ref.child(Auth.auth().currentUser?.uid ?? "none").setValue(user)
你可以试着用类似的东西替换它,根据你的需要调整:
if let userid = Auth.auth().currentUser?.uid {
self.ref.child("users/(userid)/").setValue(user)
}