3

Getting error while appending arrays in snapshot. I want to retrieve all values from these arrays EngNames , UrNames and cakeImages in the snapshot so the values can appear on table view cells. However I can retrieve all values of Cake Rate Label and Eng Name Label from Firebse but i want just retrieve all the cakeRatelabel values of rate from firebase and rest of the data that is EngNames , UrNames and cakeImages i want to retrieve localy from defined arrays and images in assets respectively. help me to accomplish this, any help will be highly appreciated.

Firebase Structure
enter image description here

View Controller

enter image description here


TestTabelViewController

   import UIKit
   import Firebase

   class TestTableViewController: UITableViewController {

@IBOutlet var cakeTableView: UITableView!

var ref = FIRDatabase.database().reference()


struct Cake {
    let cakeEngNames: String
    let cakeUrNames: String
    let cakeImages: UIImage
    var cakeRates: String
}

var cakes = [Cake]()

var cakeImages = [UIImage(named : "bakery_almond_cake")!,UIImage(named : "bakery_azna_mental_cake")!, UIImage(named : "bakery_black_forest_cake")!, UIImage(named : "bakery_checker_cake")!, UIImage(named : "bakery_cheese_cake")!, UIImage(named : "bakery_chocolate_cake")!, UIImage(named : "bakery_coconut_macaroni_cake")!, UIImage(named : "bakery_cream_cake")!, UIImage(named : "bakery_cream_layer_cake")!, UIImage(named : "bakery_fruit_cake")!, UIImage(named : "bakery_lemon_layer_cake")!, UIImage(named : "bakery_pineapple_cake")!, UIImage(named : "bakery_plain_cake")!,UIImage(named : "bakery_pulm_cake_almond")!,UIImage(named : "bakery_rich_plum_cake")!,UIImage(named : "bakery_swiss_cake")!]

var EngNames = ["Almond Cake", "Azna cake", "Black forest Cake","checker cake", "cheese cake", "chocolate cake","coconut cake", "cream cake", "cream layer cake", "fruit cake", "lemon cake", "pine apple cake", "plain cake", "plum cake", "rich cake", "swiss cake"]

var UrNames = ["Almond Badam cake", "Azna wala cake", "Black kala forest Cake","checker  wala cake", "cheese wala cake", "chocolate wala cake","coconut wala cake", "cream wala cake", "cream wala layer cake", "fruit wala cake", "lemon wala cake", "pine apple wala cake", "plain wala cake", "plum wala cake", "rich wala cake", "swiss wala cake"]


override func viewDidLoad() {
    super.viewDidLoad()

    let almondSnap = ref.child("Hyderabad").child("Bakery").child("Cake")
    almondSnap.observe( .value, with: { (snapshot) in
        if snapshot.hasChildren(){
            for snap in snapshot.children {
               if let node = snap as? FIRDataSnapshot ,  let rate = node.value as? Int {
                    self.cakes.append(Cake(cakeEngNames: EngNames,
                                           cakeUrNames: UrNames,
                                           cakeImages: cakeImages,
                                           cakeRates: String(rate)))
                }
            }

            self.cakeTableView.reloadData()
        }
    })

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.cakes.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell

    cell.cakeImage.image = self.cakes[indexPath.row].cakeImages
    cell.cakeEngLabs.text = self.cakes[indexPath.row].cakeEngNames
    cell.cakeUrLabs.text = self.cakes[indexPath.row].cakeUrNames
    cell.cakeRateLabs.text = self.cakes[indexPath.row].cakeRates
    return cell
}
}
7
  • 2
    Which line gives you the error? Commented Mar 11, 2017 at 9:41
  • cakeEngNames: EngNames, cakeUrNames: UrNames, cakeImages: cakeImages, Commented Mar 11, 2017 at 9:43
  • self.cakes.append........................cakeEngNames: EngNames, cakeUrNames: UrNames, cakeImages: cakeImages Commented Mar 11, 2017 at 9:44
  • 2
    The error message is clear; the constructor for Cake takes a String and you are attempting to pass an array of String; you need to find the appropriate index and use that with the arrays. How do you know which cake the firebase record applies to? Commented Mar 11, 2017 at 9:46
  • @Xcodian You want only rate from Firebase? Because other details like name you have created array of it? Commented Mar 11, 2017 at 9:56

1 Answer 1

4
+100

You are passing arrays to String parameters in your initialiser for the Cake struct. Try something like

for (i, snap) in snapshot.children.enumerated() {
    if let node = snap as? FIRDataSnapshot ,  let rate = node.value as? Int {
        self.cakes.append(Cake(cakeEngNames: EngNames[i],
                               cakeUrNames: UrNames[i],
                               cakeImages: cakeImages[i],
                               cakeRates: String(rate)))
    }
}

assuming that the three arrays you define at the top, are supposed to map to the various parameters of the different cakes. Here you take the relevant entry from each array, rather than passing the whole array.

Sign up to request clarification or add additional context in comments.

3 Comments

thanx alot dear,
i will award you bounty but there is timing error in 23 hours
bounties awarded :) thanx again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.