[英]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.