-1

So I've gone through every possible solution offered on SO and tried all the solutions but my case seems to be singular. I have a tableview in a 'HomeViewController' which is connected to the HomeViewController.swift file and there's also a HomeViewControllerTableViewCell.swift file. Here's my code:

HomeViewController.swift:

import Firebase
import FirebaseDatabase
import UIKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return artistlists.count

}


    var artistlists = [ArtistList]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "celler", for: indexPath) as! HomeViewControllerTableViewCell


    let artists : ArtistList
    artists=artistlists[indexPath.row]
    print("Here")

    cell.labelName.text = artists.name
    cell.labelCity.text = artists.city
    cell.labelDate.text = artists.date

    return cell

}

@IBOutlet weak var tableArtists: UITableView!



    override func viewDidLoad() {
        super.viewDidLoad()
        var ref: DatabaseReference!
        ref = Database.database().reference()
        ref.child("Fund").observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot.childrenCount)
            if snapshot.childrenCount > 0 {
               self.artistlists.removeAll()
               for artist in snapshot.children.allObjects as! [DataSnapshot] {

                  //temporary test code
                  if let artistDict = artist.value as? [String: Any] {
                      if let aName = artistDict["artist_name"] as? String {
                          print("first name was \(aName)")
                      } else {
                          print("ARTIST NAME WAS NIL! RUN FOR YOUR LIVES!")
                      }
                  } else {
                      print("artist.value was nil!")
                  }
                  //end of temporary test code

//            let artistObject = artist.value as? [String: AnyObject]
//            let artistName = artistObject?["artist_name"]
//            let artistCity =  artistObject?["perf_location"]
//            let artistDate =  artistObject?["target_date"]
//            let artistName2:String = String(format: "%@", artistName as! CVarArg)
//            print(artistName2)
//            let artistCity2:String = String(format: "%@", artistCity as! CVarArg)
//            let artistDate2:String = String(format: "%@", artistDate as! CVarArg)
//            let artisty = ArtistList(name: artistName2 as String?, city: artistCity2 as String?, date: artistDate2 as String?)
//            self.artistlists.append(artisty)
                }

                self.tableArtists.reloadData()
            }


            // Do any additional setup after loading the view.
        })
        { (error) in
            print(error.localizedDescription)
        }
    self.tableArtists.register(HomeViewControllerTableViewCell.self, forCellReuseIdentifier: "celler")

}

HomeViewControllerTableViewCell.swift

import UIKit
import FirebaseDatabase
import Firebase
class HomeViewControllerTableViewCell: UITableViewCell {
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var labelCity: UILabel!
@IBOutlet weak var labelDate: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

ArtistList.swift:

import Foundation
class ArtistList
{
    var name: String?
    var city: String?
    var date: String?

    init(name: String?, city: String?, date: String?) {

        self.city = city
        self.date = date
        self.name = name
    }
}

I'm getting an error in HomeViewController.swift, line 28-30, saying 'Unexpectedly found nil while wrapping an optional value'.

cell.labelName.text = artists.name, 
cell.labelCity.text = artists.city, 
cell.labelDate.text = artists.date

are lines 28-30.

enter image description here

Can someone please help me out with this?

Jay
  • 34,438
  • 18
  • 52
  • 81
Arjun Ram
  • 369
  • 2
  • 18
  • Have you triple-checked that all outlets (labelName, labelCity, labelDate) are properly connected and not nil? – Martin R Jul 11 '18 at 18:25
  • How do you check if they have been connected after the connections have been made? – Arjun Ram Jul 11 '18 at 18:47
  • I would say artists.name is nil, which is likely caused by this line *let artistName = artistObject?["artist_name"]* as it has no error checking. You probably have a node in your Firebase with no 'artist_name' child. – Jay Jul 11 '18 at 22:05
  • artists.name is loading the values correctly. I checked it by printing it! – Arjun Ram Jul 12 '18 at 04:54
  • In fact, the data is correctly pulled from the database as evidenced by it correctly being printed. It's just not being shown on the labels. I double checked the labels and seem to be declared fine! – Arjun Ram Jul 12 '18 at 04:55
  • may be artist.name count is less than of ArtistList , how you defined ArtistList, can you add that in code – chandra1234 Jul 12 '18 at 06:38
  • Could you just type the code for me as to how to set a breakpoint? I'm very new to this. – Arjun Ram Jul 12 '18 at 11:36
  • The app stops before it even reaches line 40. How does the breakpoint help? – Arjun Ram Jul 12 '18 at 15:39
  • I think you missed my point. You don't have any error checking and in case there *is* an issue, such as a node that's missing "artist_name" for example, it will crash your app. You are allowing optional values in several places that could be nil and of course if they are nil then you will get that exact error. There's not a lot of options here; either a) your array is being modified elsewhere in your code making for example, the count incorrect or b) elsewhere in your code is setting one of the objects at an index to nil or c) as I said above, artists.name is nil. – Jay Jul 12 '18 at 18:40
  • Do me a favor, keeping in mind I am just trying to help narrow this down for you. I modified your question with a little bit of temporary code that includes error checking - see the section within the Firebase closure *//temporary test code* and commented out your code. Add that code to your project and comment out your code and run it. See if it catches any instances where artist_name or the object itself is nil. It takes a just a sec to put your question back to the way it was. We cannot post answers because the question was closed as a duplicate so I am hoping that will help. – Jay Jul 12 '18 at 18:53
  • First of all, thank you so much for taking all the time to modify the code and helping me out. Thanks a lot! Let me run the code and let you know how it goes soon! – Arjun Ram Jul 12 '18 at 20:20

1 Answers1

0

Broken outlet links are a very common cause of such errors. My guess is either the outlet links to labelName, labelCity, and labelDate are not connected in your HomeViewControllerTableViewCell, or your cell with the identifier "celler" is not set up as a as a cell of type HomeViewControllerTableViewCell.

Duncan C
  • 128,072
  • 22
  • 173
  • 272