使用Swift iOS从Firebase中的子子项检索所有值

时间:2017-02-23 12:24:33

标签: arrays swift xcode firebase uicollectionview

我正在尝试从Firebase的子子项中检索所有值,以便在集合视图的标签中显示。 我必须得到连接视图单元的连接插座,并有一个视图控制器有一个图像和三个标签。 在最后一个标签中,我想从firebase child获得所有价格值。

Firebase结构

enter image description here

的ViewController

enter image description here

检索值

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

}

最新错误

enter image description here

另一种方式

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

}

另一种方法错误
enter image description here

2 个答案:

答案 0 :(得分:2)

由于该值不是字典数组,因此存在字符串数值,因此误差低于此值。

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

应该是

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

fruitArray

中分配蛋糕率后,不要使用 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

希望这肯定会解决您的错误并获得您想要的输出。

答案 1 :(得分:2)

我写了一个新类和新的fetch函数。看看这对你有用,

class CakeData {
   var cakeImage : UIImage?
   var cakeEnglichName: String?
   var cakeUrduName : String?
   //var cakeRate : String
    var key : String?
 init(postKey: String, postData: Dictionary <String, Any>) {
       self.key = postKey

       //Don't forget to add your keys
       if let cakeImage   = postData["addKey"] {
          self.cakeImage = cakeImage as? UIImage
       }
       if let cakeEnglichName = postData["addKey"] {
          self.cakeEnglichName = cakeEnglichName as? String
       }
       if let cakeUrduName = postData["addKey"] {
          self.cakeUrduName = cakeUrduName as? String
       }
     }

获取功能,

func fetchCakeData() {
    let ref = FIRDatabase.database().reference()
ref.child("Hyderabad").child("Bakery").child("Cake").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
       if let cakeSnapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
           for cakes in cakeSnapshot {
               if let cakeDictionary = cakes.value as? Dictionary <String, Any> {
                   let key = cakes.key as String
                   //your class that takes a key as String and a Dictionary
                   let post = CakeData(postKey:key, postData: cakeDictionary)
                   self.cakeArray.append(post)
               }
           }
       }
      cakeCollectionView.delegate = self
     cakeCollectionView.dataSource = self
   })
}

这是根据你的代码,

import UIKit
import Firebase

struct cakeObject {
    var cakeImage : UIImage
    var cakeEnglichName: String
    var cakeUrduName : String
    // var cakeRate : String
    var key : String
}
class CakeData {
    var cakeImage : UIImage?
    var cakeEnglichName: String?
    var cakeUrduName : String?
    //var cakeRate : String
    var key : String?
    init(postKey: String, postData: Dictionary <String, Any>) {
        self.key = postKey

        //Don't forget to add your keys
        if let cakeImage   = postData["addKey"] {
            self.cakeImage = cakeImage as? UIImage
        }
        if let cakeEnglichName = postData["addKey"] {
            self.cakeEnglichName = cakeEnglichName as? String
        }
        if let cakeUrduName = postData["addKey"] {
            self.cakeUrduName = cakeUrduName as? String
        }
    }
}


class CakeViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var manuCake: UIBarButtonItem!
    @IBOutlet weak var cakeCollectionView: UICollectionView!
    var cakeArray: [CakeData] = []
// var cakeArray : [cakeObject] = []

    override func viewDidLoad() {
        super.viewDidLoad()

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

    override func viewWillAppear(_ animated: Bool) {
        fetchCakeData()
    }

    func fetchCakeData() {
        let ref = FIRDatabase.database().reference()
        ref.child("Hyderabad").child("Bakery").child("Cake").observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
            if let cakeSnapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for cakes in cakeSnapshot {
                    if let cakeDictionary = cakes.value as? Dictionary <String, Any> {
                        let key = cakes.key as String
                        //your class that takes a key as String and a Dictionary
                        let post = CakeData(postKey:key, postData: cakeDictionary)
                        self.cakeArray.append(post)
                    }
                }
            }
            self.cakeCollectionView.reloadData()
        })
    }




 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



        print("Cake : \(cakeArray[indexPath.row])")
        cell.cakeImageView.image = cakeArray[indexPath.item].cakeImage
        cell.cakeEngLabs.text = cakeArray[indexPath.item].cakeEnglichName


        return cell 
    }


}