尝试将用户电子邮件和名称保存到Firebase数据库时出现异常

时间:2016-11-04 08:57:54

标签: ios objective-c swift firebase firebase-realtime-database

我在swift 3.0中处理项目,并且在尝试将两个字符串值保存到Firebase数据库时,我得到以下异常

  

由于未捕获的异常'InvalidFirebaseData'而终止应用程序,原因:'(updateChildValues:withCompletionBlock :)无法存储_SwiftValue类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。'

下面的类的代码。 我缺少什么?

import UIKit
import Firebase

class ViewController: UIViewController {
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var userEmailTextField: UITextField!
    @IBOutlet weak var passWordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
//   
   }
@IBAction func loginPressed(_ sender: AnyObject) {
 FIRAuth.auth()?.createUser(withEmail: userEmailTextField.text!, password: passWordTextField.text!, completion: { (user :FIRUser?, error) in
             if error != nil {

                print("error in login")
                return
             }else{
             print("loggin success")

             let ref = FIRDatabase.database().reference(fromURL: "https://chatsample-4cc11.firebaseio.com/")
                let values = ["username" : self.userNameTextField.text, "email" : self.userEmailTextField.text]
                ref.updateChildValues(values, withCompletionBlock: { (err, ref) in
                    if err != nil {
                    print(err)
                        return
                    }
                    print("saved user successfully to firebase DB")
                })
                //ref.updateChildValues(values)
             //ref.updateChildValues(["somevalue": 123123])

             }
            })
    }

}

1 个答案:

答案 0 :(得分:1)

我认为问题是你没有打开用户名和电子邮件的值,所以这些值是Optional的类型。你应该改为

let values = ["username" : self.userNameTextField.text!, "email" : self.userEmailTextField.text!]