不能在火力地堡
问题描述:
得到节点的值我读了很多例子,他们大多有旧式(即使它们被写入当年)。请帮忙了解我的代码错在哪里?它是建立,但我不能让一个价值123456
import UIKit
import Firebase
class ViewController: UIViewController {
@IBOutlet weak var val_txt: UITextField!
let ref = FIRDatabase.database().reference()
var barcode = 0
override func viewDidLoad() {
super.viewDidLoad()
FIRAuth.auth()?.signIn(withEmail: "*****@gmail.com", password: "*****", completion: {(user,error) in print("Авторизация Ок!!!")})
}
@IBAction func getData_btn(_ sender: Any) {
ref.child("goods").child("1").observe(FIRDataEventType.value, with: {(snapshot) in
let postDict = snapshot.value as? [String:AnyObject] ?? [:]
print(postDict["barcode"] as? Int)
})
print(barcode)
}
我已经改变的代码,以便了解是否打印执行,我发现它不
print("Method started")
ref.child("goods").child("1").observe(FIRDataEventType.value, with:{(snapshot) in
let postDict = snapshot.value as? [String:AnyObject] ?? [:]
print("Method is executing")
})
print("Method completed")
我也得到打印
"Method started"
"Method completed"
答
如果你想只为 “1” 值:
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("goods").child("1").observeSingleEvent(of: .value, with: { (snapshot) in
let id = snapshot.childSnapshot(forPath: "barcode")
print(id)
})
,但如果你希望所有条形码:
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("goods").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots
{
let barcode = snap.childSnapshot(forPath: "barcode").value! as! String
print(barcode)
}
}
})
答
的只是两行代码let ref = FIRDatabase.database().reference()
没有指向引用URL,因为在初始化过程中你的火力点不在你的应用程序委托文件中配置(不叫FIRApp.configure()
)。
所以放在func viewDidLoad()
如下:
import UIKit
import Firebase
class ViewController: UIViewController {
@IBOutlet weak var val_txt: UITextField!
let ref:FIRDatabaseReference!
var barcode = 0
override func viewDidLoad() {
super.viewDidLoad()
ref:FIRDatabaseReference = FIRDatabase.database().reference()
FIRAuth.auth()?.signIn(withEmail: "*****@gmail.com", password: "*****", completion: {(user,error) in print("Авторизация Ок!!!")})
}
+0
不幸的是,没有工作:( – LEONID
后期图像你的firebase结构 –
准备好链接我的结构 – LEONID
更新之后上面的评论它sa是我的“类ViewController没有初始化器”,并在viewDidLoad成为错误:“预期表达式” – LEONID