-1

I've some strings and 2 arrays stored in Firestore, and now I'm trying to receive them but I've no idea how. Well, the strings works fine, but I don't know how I can query the arrays. I'm getting the error code:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

And here is my code:

                            //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()
                    }

The arrays are stored as Double, and the point is to receive it as a double, and convert it into CLLocationCoordinate2D again, so I can use the coordinates for a map. If you know how to solve this issue, please let me know. :)

Inside a class:

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
    }

Inside history-cell

    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 data: na

Thanks in advance. :)

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645

2 Answers2

3

Try replacing the following lines of code :

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

Cause: Accessing data with wrong keys

Saurabh
  • 704
  • 1
  • 7
  • 26
-1

have you tried like below?

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

Actually you are accessing data with wrong keys... "latitude" , "longitude" are valid keys but you are retrieving with "Latitude" , "Longitude" which does not exist in your data snapshot

Note Please check when storing data you are sending it in form of array of double.

Abu Ul Hassan
  • 1,076
  • 8
  • 24
  • This is when I'm saving it: ` var latitudes:[Double] = [] var longitudes:[Double] = []` I'll try what you said, but I think I already tried that tho :/ –  Mar 06 '19 at 12:20
  • so you are saving empty array of double ? – Abu Ul Hassan Mar 06 '19 at 12:22
  • also please show me data dictionary you are saving. – Abu Ul Hassan Mar 06 '19 at 12:23
  • Well, first I've this. (On my first view, where I save it to Firestore) ``var locations: [CLLocationCoordinate2D] = [] var latitudes:[Double] = [] var longitudes:[Double] = [] ViewDidLoad() { latitudes = locations.map({ $0.latitude }) longitudes = locations.map({ $0.longitude }) } SaveToFireStore() { //A lot of code before this, but nothing that includes lat and long. "latitude": latitudes, "longitude": longitudes ] } `` –  Mar 06 '19 at 12:32
  • check edited answer actually you are accessing data with wrong keys... "latitude" , "longitude" are valid keys but you are retrieving with "Latitude" , "Longitude" which does not exist in your data snapshot . – Abu Ul Hassan Mar 06 '19 at 13:12