gpt4 book ai didi

swift - 尝试将数据附加到子值时应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:00 26 4
gpt4 key购买 nike

我正在按照 firebase 中显示的说明进行操作,但即使在确保文本条目的类型为 String 之后,我仍然遇到崩溃。

这是错误:

Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''

代码如下:

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth

class BioEditViewController: UIViewController {

@IBOutlet weak var bioTextView: UITextView!
let databaseRef = FIRDatabase.database().reference()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
let userID = FIRAuth.auth()?.currentUser?.uid
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user level
let userBio = snapshot.value!["userBio"] as! String
self.bioTextView.text = userBio

})
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func doneSave(sender: UIButton) {
let textView = bioTextView.text
self.dismissViewControllerAnimated(false, completion: nil)
databaseRef.child("users/(user.uid)/userBio").setValue(textView)


}
}

我只是想更新一个特定的子对象:userBio,而不影响整个对象。

最佳答案

警告

避免将数据库引用实例化到超出范围的变量。原因:- 在你的范围之外,当你将一个类实例化为一个变量时,你不知道你的 FIRApp 是否已经配置,或者一般情况下该类是否已经初始化。只需提供对变量的引用 (!) 并稍后在范围内实例化。

改变:-

let databaseRef = FIRDatabase.database().reference()

let databaseRef = FIRDatabaseReference!

在使用之前将其初始化为:-

databaseRef = FIRDatabase.database().reference()

尝试:-

 @IBAction func doneSave(sender: UIButton) {
let textView = bioTextView.text
self.dismissViewControllerAnimated(false, completion: nil)
databaseRef = FIRDatabase.database().reference()
databaseRef.child("users/\(FIRAuth.auth!.currentUser!.uid)/userBio").setValue(textView)
}

关于swift - 尝试将数据附加到子值时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com