将日期和时间上载到Firebase时出错

时间:2017-04-23 02:44:06

标签: ios swift firebase firebase-realtime-database firebase-authentication

当用户创建帐户时,我希望他们在服务器上注册的时间和日期。尝试执行此操作时出现以下错误。

  

只能存储NSNumber,NSString,NSDictionary和    NSArray的' .... libc ++ abi.dylib:以NSException(lldb)类型的未捕获异常终止

@IBAction func registerTapped(_ sender: Any) {
    let namec = nameTextField.text
    if let email = emailTextField.text, let pass = passwordTextField.text, let name = namec?.capitalized {
        FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
            if user != nil {
                //user found


                let interval = NSDate().timeIntervalSince1970
                let date = NSDate(timeIntervalSince1970: interval)



                self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": date])

                print("User Created And Added To Database", email, name, date)
                self.performSegue(withIdentifier: "registertologin", sender: self)
            }

            }

1 个答案:

答案 0 :(得分:1)

您的错误相对容易解决,您尝试将类型为NSDate的值保存到firebase中this answer says 你需要将你的价值存储为数字

let interval = NSDate().timeIntervalSince1970

当你想要阅读它时,你会这样读,所以你的代码应该是这样的

let date = NSDate(timeIntervalSince1970: interval)

所以你的代码应该是这样的

@IBAction func registerTapped(_ sender: Any) {
let namec = nameTextField.text
if let email = emailTextField.text, let pass = passwordTextField.text, let name = namec?.capitalized {
    FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
        if user != nil {
            //user found


             let interval = NSDate().timeIntervalSince1970
             // let date = Date(timeIntervalSince1970: interval)





            self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": interval])


            self.performSegue(withIdentifier: "registertologin", sender: self)
        }

        }

或者你可以做的最简单的事情,你可以避免这种类型的错误是将它存储为一个字符串

@IBAction func registerTapped(_ sender: Any) {
    let namec = nameTextField.text
    if let email = emailTextField.text, let pass = passwordTextField.text, let name = namec?.capitalized {
        FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
            if user != nil {
                //user found


                 let interval = NSDate().timeIntervalSince1970
                 let date = Date(timeIntervalSince1970: interval)
                 let dateFormatter = DateFormatter()
                 dateFormatter.dateFormat = "dd/MM/yyyy" // you can change the date format to whatever you want
                 let dateString = dateFormatter.string(from: date)




                self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString])


                self.performSegue(withIdentifier: "registertologin", sender: self)
            }

            }
PS:还有一些其他最简单的方法可以解决这个问题我不会包含在我的答案中所以我可以让搜索和学习并记住一个好的代码是一个不应该重复自己的重构代码(不要重复)重复一遍!)。