Swift:无法在childByAutoId

时间:2019-04-18 08:54:12

标签: swift firebase

我正在尝试从Firebase检索数据,但是一旦添加setValueForKeys方法,应用程序就会崩溃。

在此之前,我尝试了其他各种方法来检索数据,但是无法访问存储在childByAutoId下的数据

databaseImage

我需要访问“工作”部分下存储的开始值和结束值

这是保存数据的方式:

func post () {

    let dateFormatter = DateFormatter()
        dateFormatter.timeStyle = .none
        dateFormatter.dateStyle = .medium

    let todaysDate = dateFormatter.string(from: date)



    let post: [String : String] = ["start" : startTimeField.text!, "end" : endTimeField.text!]


    let databaseRef = Database.database().reference()

        databaseRef.child("Posts").child("\(todaysDate)").child(categoryField.text!).childByAutoId().setValue(post)


}

为了获取数据,我创建了一个Object类

import UIKit

class Category: NSObject {

    var start : String?
    var end: String?

}

这是表视图控制器上的参考

 var postData = [Category]()

检索方法在

下方
 func fetchData () {

        Database.database().reference().child("Posts").child("Apr 18, 2019").child("Work").observeSingleEvent(of: .childAdded, with: { (snapshot) in

           if let dictionary = snapshot.value as? [String: String] {
                let category = Category()

            category.setValuesForKeys(dictionary)

            category.start = dictionary["start"]
            category.end = dictionary["end"]

            self.postData.append(category)

            self.tableView.reloadData()


            }



       }, withCancel: nil)

    }

表视图的代码也在

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

    return postData.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: postCell)

    let category = postData[indexPath.row]
    cell.textLabel?.text = category.start

    return cell

}

0 个答案:

没有答案
相关问题