簡體   English   中英

Firebase和Swift 3代碼不再執行

[英]Firebase and Swift 3 code not executing anymore

我試圖通過func將代碼func轉換為Swift3。我不得不說我之前有完整的項目。 現在,我遇到的問題是我沒有錯誤,只有一些警告,但是某些功能沒有執行。 是什么原因造成的?

我僅假定那些給定的功能有問題,因為這些是我什至都無法print

這些是我以前使用過的一些功能,但不適用於Swift 3:

//With this I get selected brand products values like product name, nicotine, flavor etc..
 let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
      ref.observeSingleEvent(of: .value, with: { (snapshot) in
           if snapshot.exists(){
                if let products = (snapshot.value as AnyObject).allValues as? [[String:AnyObject]]{
                    self.productsValue = products
                    self.productsTable.reloadData()
                }
           }
  })

//With this fucntion I get the products count.
    let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: filteredBrands[indexPath.row])
                ref.observeSingleEvent(of: .value, with: { (snapshot) in
                    if snapshot.exists(){
                        if let products = (snapshot.value as AnyObject).allValues as? [[String:AnyObject]]{

                                var count = (snapshot.childrenCount)
                                snusProductCountLabel.text = "\(count) products"

                        }
                    }
                })

//Parse snus brands
    func parseSnuses(){
        let ref = FIRDatabase.database().reference().child("Brands").queryOrderedByKey()

        ref.observe(.childAdded, with: { (snapshot) in
            self.brands.append(snapshot.key)
            print(snapshot.key)
            self.snusBrandsTableView.reloadData()
        }){ (error) in

        }

我可以做的任何不同的事,請告訴我! 這些函數在不同的ViewControllers

編輯:這是我的JSON樹

{
  "Snuses" : {
    "Catch Eucalyptus White Large" : {
          "Brand" : "Catch",
          "Products" : "Catch Eucalyptus White Large",
          "PorionWeight" : 21.6,
          "flavor" : "Tobacco, Eucalyptus",
          "nicotine" : 8.0,
          "PortionsCan" : 24,
          "shipping weight" : 39
        },

這些是安全規則:

{ 
  "rules": { 
     ".read": "true", 
     ".write": "true", 
     "Snuses": { 
        ".indexOn": "Brand"
     } 
  } 
}

我相信

if let products = (snapshot.value as AnyObject)
                   .allValues as? [[String:AnyObject]]{

是問題。

嘗試進行以下測試,以查看其是否從快照中打印數據:

 let ref = FIRDatabase.database().reference().child("Snuses")
               .queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
      ref.observeSingleEvent(of: .value, with: { (snapshot) in
           if snapshot.exists() {

                let dict = snapshot?.value as! [String: [String:String]]
                let productsArray = Array(dict)

                for row in productsArray {
                     print(row)
                }
           }
 })

對於非敏捷測試,您也可以在閉包內部嘗試使用此方法,而不是上面的方法

let d2 = snapshot?.value as! NSDictionary
let a2 = d2.allValues

for r2 in a2 {
     print(r2)
}

另一種選擇:

let q = snapshot?.value as! [String: AnyObject]
let a3 = Array(q)

for r3 in a3 {
     print(r3)
}

我不知道您的tableView在數組中期望什么,但是其中之一應該覆蓋它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM