【问题标题】:Firebase and Swift 3 code not executing anymoreFirebase 和 Swift 3 代码不再执行
【发布时间】:2017-02-10 10:39:53
【问题描述】:

我尝试将我的代码 func 转换为 func 到 Swift 3。我不得不说我之前有完整的工作项目。现在我遇到了问题,我没有错误,只有一些警告,但有些功能没有被执行。这应该是什么原因造成的?

我只假设那些给定的功能是错误的,因为这些是我什至没有得到任何东西的部分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"
     } 
  } 
}

【问题讨论】:

  • 您到底看到了什么,就错误而言?不清楚是什么问题...
  • 没有错误只是代码没有给出任何东西。
  • 那么“代码没有给出任何东西”是什么意思呢?代码应该“给你”什么?
  • 使用您的 JSON 树更新您的问题,并且您的安全规则还会说明您的用户在使用任何功能之前是否经过身份验证。其次它太宽泛了......
  • 我更新了我的问题。在代码 cmets 中,您会看到山雀应该给出什么。例如,在最后一个中,它应该给出所有“品牌”,例如“Catch”。

标签: ios swift firebase tableview firebase-realtime-database


【解决方案1】:

我相信

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)
                }
           }
 })

对于非 swifty 测试,您也可以在闭包中尝试这个而不是上面的

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 在数组中期望什么,但其中一个应该涵盖它。

【讨论】:

  • 这会产生新的问题,例如:'InvalidQueryParameter',原因:'你只能将 nil、NSString 或 NSNumber 传递给 queryEqualToValue:'如果你愿意的话,我可以在 Github 上分享我的 repo 并有时间。
  • 在执行该代码之前,您需要将 brandName = 定义为某些内容。该代码对我有用。
  • 绝对 :D 我做到了。我的“品牌名称”是 indexPath.row。我做到了。
  • 这可能就是问题所在。取出 indexPath.row 并替换实际字符串 .queryEqual(toValue: "Catch") 因为这适用于我上面的所有示例并返回快照。
  • 还是什么都没有……啊……什么都没有。我不明白为什么它在 Swift 2 中有效?从那以后什么都没有改变,逻辑和功能都是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
相关资源
最近更新 更多