从Firestore查询阵列

时间:2019-03-06 11:18:23

标签: ios arrays swift google-cloud-firestore

我在Firestore中存储了一些字符串和2个数组,现在我正试图接收它们,但是我不知道如何。好的,字符串工作正常,但是我不知道如何查询数组。我收到错误代码:

  

线程1:致命错误:在展开可选值时意外发现nil

这是我的代码:

                            //For-loop
                        for document in QuerySnapshot!.documents
                        {
                            self.list.removeAll()
                            //let document = QuerySnapshot!.documents
                            let data = document.data()

                            let data1 = data["Dog"] as? String
                            let data2 = data["Person"] as? String
                            let data3 = data["What"] as? String
                            let data4 = data["Time"] as? String
                            let data5 = data["Date"] as? String
                            let data6 = data["Kilometers"] as? String
                            let data7 = data["Timer"] as? String
                            let data8 = data["Latitude"] as? Array<Double>
                            let data9 = data["Longitude"] as? Array<Double>

                            let txt = listTxt(dog: data1!, person: data2!, action: data3!, time: data4!, date: data5!, kilometers: data6!, timer: data7!, latitude: data8 ?? [], longitude: data9 ?? [])

                            print(txt)

                            tempTxt.append(txt)

                        }
                        self.list = tempTxt
                        self.tableView.reloadData()
                    }

数组存储为 Double ,关键是将其接收为double,然后再次将其转换为CLLocationCoordinate2D,因此我可以将坐标用于地图。如果您知道如何解决此问题,请告诉我。 :)

在班级内:

class listTxt
{
    var dog: String
    var person: String
    var action: String
    var time: String
    var date: String
    var kilometers: String
    var timer: String
    var latitude: [String]
    var longitude: [String]

    init(dog: String, person: String, action: String, time: String, date: String, kilometers: String, timer: String, latitude: [String], longitude: [String])
    {
        self.dog = dog
        self.person = person
        self.action = action
        self.time = time
        self.date = date
        self.kilometers = kilometers
        self.timer = timer
        self.latitude = latitude
        self.longitude = longitude
    }

内部历史记录单元格

    class HistoryCell: UITableViewCell
{

    @IBOutlet weak var DogName: UILabel!
    @IBOutlet weak var Person: UILabel!
    @IBOutlet weak var Action: UILabel!
    @IBOutlet weak var Date: UILabel!
    @IBOutlet weak var Time: UILabel!
    @IBOutlet weak var timer: UILabel!
    @IBOutlet weak var kilometer: UILabel!
    var latitude: [String] = []
    var longitude: [String] = []

    func setCell(list: listTxt)
    {
        DogName.text = list.dog
        Person.text = list.person
        Action.text = list.action
        Date.text = list.date
        Time.text = list.time
        timer.text = list.timer
        kilometer.text = list.kilometers
        latitude = list.latitude
        longitude = list.longitude
    }
}

Firestore数据: na

先谢谢了。 :)

2 个答案:

答案 0 :(得分:4)

尝试替换以下代码行:

let data8 = data["latitude"] as? [Double]
let data9 = data["longitude"] as? [Double]

原因:使用错误的键访问数据

答案 1 :(得分:0)

您尝试过以下方法吗?

 let data8 = data["latitude"] as? [Double]
 let data9 = data["longitude"] as? [Double]

实际上,您正在使用错误的键访问数据...“纬度”,“经度”是有效的键,但是您正在使用数据快照中不存在的“纬度”,“经度”进行检索

注意:存储数据时,请检查以double数组形式发送的数据。