在通过segue传递数据时,在解开Optional值时意外地发现了nil

时间:2017-02-22 03:31:45

标签: ios swift uitableview firebase segue

我正在尝试在按下segue时从Firebase发送数据。但是,每当我点击UITableViewCell时,我都会收到错误"意外地发现nil,同时展开一个Optional值"在FIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in

这是我的prepareForSegue

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
    if (segue.identifier == "showDetails") {
        let detailsViewController = segue.destination as! PostDetailsViewController
        let indexPath = self.postsTableView.indexPathForSelectedRow
        let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
        detailsViewController.postDetails = post["postID"] as? String
    }
}

这是我试图将数据传递给

的单独的ViewController
var postDetails: String?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    FIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            self.priceLabel.text = dictionary["price"] as? String
        }
    })
}

我在这里做错了什么可能导致这个?

0 个答案:

没有答案