我正在尝试从Firebase的子子级检索所有值,以在集合 View 中的标签中显示。
我必须获得到Collection View Cell的连接 socket ,并具有一个具有图像和三个标签的View Controller 。
在上一个标签中,我想从firebase child获得所有价格值。

火力基地的结构

arrays - 使用Swift iOS从Firebase中的子 child 检索所有值-LMLPHP

ViewController

arrays - 使用Swift iOS从Firebase中的子 child 检索所有值-LMLPHP

检索值

  import UIKit
  import  Firebase
 class CakeViewController:  UIViewController , UICollectionViewDelegate,   UICollectionViewDataSource {

@IBOutlet weak var manuCake: UIBarButtonItem!

@IBOutlet weak var cakeCollectionView: UICollectionView!


struct cakeObject {

    var cakeImage : UIImage
    var cakeEnglichName: String
    var cakeUrduName : String
    var cakeRate : String
    var key : String



}

var cakeArray : [cakeObject] = []



override func viewDidLoad() {
    super.viewDidLoad()

    manuCake.target = revealViewController()
    manuCake.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())



    let ref = FIRDatabase.database().reference().child("Hyderabad").child("Bakery").child("Cake")

    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists() {


            print(snapshot.value as Any)
            let cakeData = snapshot.value as! [String:String]
                for (key,value) in cakeData{
                    //Can set cakeImage and cakeUrduName as per your project
                    cakeArray.append(cakeObject(cakeImage: #imageLiteral(resourceName: "bakery_almond_cake"),cakeEnglinhName: key, cakeUrduName: "آلمنڑ کیک", cakeRate: "\(value)/kg"))
                }
                self.cakeCollectionView.reloadData()
            }

    })



    cakeCollectionView.delegate = self
    cakeCollectionView.dataSource = self

    // Do any additional setup after loading the view.
}





func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return cakeArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CakeCollectionViewCell", for: indexPath)
        as! CakeCollectionViewCell

    cell.cakeImageView.image = cakeArray[indexPath.item].cakeImage
    cell.cakeEngLabs.text = cakeArray[indexPath.item].cakeEnglichName
    cell.cakeUrLabs.text = cakeArray[indexPath.item].cakeUrduName
    cell.cakeRateLabs.text =  cakeArray[indexPath.item].key

    return cell

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

最新错误

arrays - 使用Swift iOS从Firebase中的子 child 检索所有值-LMLPHP

同类同学
 import UIKit
 import  Firebase
 class CakeViewController:  UIViewController , UICollectionViewDelegate,   UICollectionViewDataSource {

@IBOutlet weak var manuCake: UIBarButtonItem!

@IBOutlet weak var cakeCollectionView: UICollectionView!


struct cakeObject {

    var cakeImage : UIImage
    var cakeEnglichName: String
    var cakeUrduName : String
    //var cakeRate : String
    var key : String



}

var cakeArray : [cakeObject] = []



override func viewDidLoad() {
    super.viewDidLoad()

    manuCake.target = revealViewController()
    manuCake.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())



    let ref = FIRDatabase.database().reference().child("Hyderabad").child("Bakery").child("Cake").child("Almond_Cake")

    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists() {


            print(snapshot.value as Any)
             let cakeData = snapshot.value as! [String:String]
                for (key,value) in cakeData{
                    //Can set cakeImage and cakeUrduName as per your project
                    self.cakeArray.append(cakeObject(cakeImage: #imageLiteral(resourceName: "bakery_almond_cake"),cakeEnglichName: key, cakeUrduName: "آلمنڑ کیک", key: "\(value)/kg"))
                }
                self.cakeCollectionView.reloadData()
            }
       })



    cakeCollectionView.delegate = self
    cakeCollectionView.dataSource = self

    // Do any additional setup after loading the view.
}





func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return cakeArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CakeCollectionViewCell", for: indexPath)
        as! CakeCollectionViewCell

    cell.cakeImageView.image = cakeArray[indexPath.item].cakeImage
    cell.cakeEngLabs.text = cakeArray[indexPath.item].cakeEnglichName
    cell.cakeUrLabs.text = cakeArray[indexPath.item].cakeUrduName
    cell.cakeRateLabs.text =  cakeArray[indexPath.item].key

    return cell

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

另一种方式的错误

arrays - 使用Swift iOS从Firebase中的子 child 检索所有值-LMLPHP

最佳答案

您将收到以下错误消息,因为该值不是字典数组,值是字符串。

if let cake = snapshot.value![a] as? [[String:String]]

它应该是
if let cake = snapshot.value![a] as? String

中分配蛋糕率后,看不到 fruitArray 的使用

我建议您按照以下代码填充cakeArray:
print(snapshot.value as Any)
let cakeData = snapshot.value as! [String:String]
    for (key,value) in cakeData{
           //Can set cakeImage and cakeUrduName as per your project
           cakeArray.append(cakeObject(cakeImage: #imageLiteral(resourceName: "bakery_almond_cake"),cakeEnglinhName: key, cakeUrduName: "آلمنڑ کیک", cakeRate: "\(value)/kg"))
      }
    self.cakeCollectionView.reloadData()

第二个错误:我没有看到任何对象作为Cake结构的键。所以编译器会给出错误。您在上一行中为cakeRateLabs分配值。因此,无需再次分配,只需删除/注释cell.cakeRateLabs.text = cakeArray[indexPath.item].key即可。

希望这一定会解决您的错误并根据需要获取输出。

关于arrays - 使用Swift iOS从Firebase中的子 child 检索所有值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42415705/

10-08 22:45