![](/img/trans.png)
[英]Why isn't Firebase library importing into my iOS Swift application?
[英]Why can't my iOS application read from Firebase Database?
我目前在讀取和將Firebase數據庫顯示到表列表中時遇到麻煩。 以下是我在當前View Controller中使用的代碼。
import UIKit
import Firebase
import FirebaseDatabase
class Buy_1: UIViewController, UITableViewDataSource,
UITableViewDelegate {
var ref: DatabaseReference!
var databaseHandle: DatabaseHandle!
var postItem: [String] = []
@IBOutlet weak var TableView: UITableView!
@IBAction func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindSegueToVC1", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
//Set Self as tableview's data source and delegate
self.TableView.delegate = self
self.TableView.dataSource = self
//Set the firebase reference
ref = Database.database().reference()
//Retrieve and listen for changes
databaseHandle = ref?.child ("items").observe(.childAdded, with: { (snapshot) in
//Convert value of data into string
if let item = snapshot.value as? String
{
self.postItem.append(item)
self.TableView.reloadData()
self.ref?.keepSynced(true)
}
/*if let actualItem = item {
//Appends data to our Item Array
self.postItem.append(actualItem)
//Reload TableView
self.TableView.reloadData()
}*/
})
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
//MARK: - UITableView Delegate Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postItem.count
}
func tableView(_ TableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = TableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath)
cell.textLabel?.text = postItem[indexPath.row]
return cell
}
}
我沒有在代碼中收到任何錯誤,所以我不確定是什么錯誤。 &我已經將數據庫規則設置為true。
謝謝。
您正在觀察.childAdded
,僅當將子級添加到觀察到的數據樹中時,它才會觸發塊。 將其更改為.value
。
代碼中的問題是項的子項不是字符串。 它們本身就是childSnapshots或key:value對。
這是一些示例代碼,用於從類似於您的結構中讀取itemName
items
item_0
itemName: "Josh"
item_1
itemName: "Screaming Eagle"
item_2
itemName: "Insignia"
以及用於打印每個子節點的鍵和itemName的代碼
let ref = self.ref.child("items")
ref.observe(.childAdded, with: { snapshot in
if snapshot.exists() {
let dict = snapshot.value as! [String: Any]
let itemName = dict["itemName"] as? String ?? "No Item Name"
print(snapshot.key, itemName)
}
})
和輸出
item_0 Josh
item_1 Screaming Eagle
item_2 Insignia
請注意,如果未找到itemName,我使用nil合並運算符(??)來填充itemName。
我將快照的值部分視為字典,以允許您訪問子節點,但您也可以使用.childSnapshot這樣來完成
let i = snapshot.childSnapshot(forPath: "itemName").value as? String ?? "No Item"
在此特定示例中,snapshot.exists是多余的,但應與按.value讀取一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.