didayo didayo -3 years ago 118
Swift Question

Error while receiving image array from the Child's folder Firebase Swift

I am currently trying to receive an array of images with title from my Child's folder to the another offertableview which is connected by button from the detailViewController, but unfortunately I keep getting an error. Below I attached images of my firebase data structure and my mainstoryboard screenshot.

For the first table view I have a list of the restaurants and upon selecting a cell it transfers to the detail view controller which lists all the details of the restaurant (for that I've created a model of my restaurant) in that detailVC I have a button connected to the offerstableview which lists all the offers of that particular restaurant.

When I click to the button it transfers to the offers table view which results to the application shut down due to the error.

my offers tableview code:

var ref: DatabaseReference!
var offerImageArray = [String]()
var titleArray = [String]()

override func viewDidLoad() {
super.viewDidLoad()
fetchBars()
}

func fetchBars(){

ref.child("Paris").observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "offers")
let dict = imageSnap.value as! [String: Any]
let imageUrl = dict["offer_image"] as? String
let titleUrl = dict["offer_title"] as? String
self.offerImageArray = [imageUrl! as String]
self.titleArray = [titleUrl! as String]
}
})
self.tableView.reloadData()
}


override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

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

cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
cell.titleLabel.text = titleArray[indexPath.row]
return cell
}


Xcode error:
2017-08-16 10:26:33.652 Applic[1174] [Firebase/Analytics][I-ACS003007] Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist
2017-08-16 10:26:33.826 Applic[1174] [Firebase/Analytics][I-ACS032003] iAd framework is not linked. Search Ad Attribution Reporter is disabled.
2017-08-16 10:26:33.828 Applic[1174] [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
fatal error: unexpectedly found nil while unwrapping an Optional value

Firebase data structure

mainstoryboard

Answer Source

Replace your OfferTableVc with following code and you will be able to display images and remove datasource and delegate provided from storyboard as its not required in your case I had given using code

//
//  OffersTableVC.swift
//  InKz
//
//  Created by Didar Naurzbayev on 14/08/2017.
//  Copyright © 2017 Didar Naurzbayev. All rights reserved.
//

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import SDWebImage


class OffersTableVC: UITableViewController {
    var ref: DatabaseReference!
   var offerImageArray = [String]()
    var titleArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchBars()
    }

    func fetchBars(){

        Database.database().reference().child("aktau").observeSingleEvent(of: .value, with: { (snapshot) in

            print("Main Snapshot is \(snapshot)")

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let imageSnap = snap.childSnapshot(forPath: "offers")



                if let snapDict = imageSnap.value as? [String:AnyObject] {
                    let dictKeys = [String](snapDict.keys)
                    print(dictKeys)
                    let dictValues = [AnyObject](snapDict.values)
                    print(dictValues)


                    for each in dictValues{
                        let imageUrl = each["offer_image"] as? String
                        print(imageUrl!)
                        self.offerImageArray.append(imageUrl!)

                    }
                    self.tableView.dataSource = self
                    self.tableView.delegate = self
                    self.tableView.reloadData()

                }





//                let dict = imageSnap.value as! [String: Any]
//                let imageUrl = dict["offer_image"] as? String
//                let titleUrl = dict["offer_title"] as? String
//                self.offerImageArray = [imageUrl! as String]
//                self.titleArray = [titleUrl! as String]
            }
        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return offerImageArray.count
    }

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

        cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
//        cell.titleLabel.text = titleArray[indexPath.row]
               return cell
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download