Tableview引发“在展开可选值时意外找到nil”

时间:2018-07-11 18:21:59

标签: ios swift uitableview firebase

因此,我已经研究了SO提供的所有可能的解决方案,并尝试了所有解决方案,但我的情况似乎很单一。我在连接到HomeViewController.swift文件的“ HomeViewController”中有一个表格视图,并且还有一个HomeViewControllerTableViewCell.swift文件。这是我的代码:

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

我在HomeViewController.swift的第28-30行出现错误,说“在包装可选值时意外发现nil”。

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

是第28-30行。

enter image description here

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

断开的出口链接是导致此类错误的常见原因。我的猜测是您的HomeViewControllerTableViewCell中没有连接到labelName,labelCity和labelDate的出口链接,或者您的标识符为“ celler”的单元未设置为类型为HomeViewControllerTableViewCell的单元