Displaying data from firestore database

I have been struggling for several weeks on how to display data from the Firestore database in a tableview or collection view. When I say I am struggling I mean I am really struggling. I have been working on this for many many hours, trying everything I can find on google.

I can retrieve the data from Firestore into a dictionary [Date: [String: String, String: int]]. But I can’t figure out how to get this information into the cell in the tableview or collection view.

In the cellForItemAtIndexPath function when I try to populate the cell with an item from the dictionary I get:

Cannot subscript a value of type ‘[Date : [String : Any]]’ with an argument of type ‘Int’

Any suggestions would be greatly appreciated!

Sorry, a correction for the above:

The dictionary is [Date: [String: Any]].

I saw one solution that stated it was because dictionaries are accessed by keys, not integer index and to extract the keys into an array and use that to access the relevant object out of the dictionary.

I was able to create an array of the keys but essentially get the same error in this line of code:

cell.historyLabel.text = keys[indexPath.row]

Cannot subscript a value of type ‘Dictionary<Date, [String : Any]>.Keys’ with an argument of type ‘Int’

can you show a screenshot of your code, including sample data from your database?

Thanks, since posting the question I re-wrote the code to create an array for each piece of data I was collecting from the database and it worked. But i really need to present the data together, so I need to save it as a dictionary.

I went back and tried to re-enter my code for the dictionary as I had it when I posted the question but I couldn’t remember it exactly as it was written before. But I still get the same error message with the attached code.

In the database the document structure is [document ID: [“Date”: date, “Engine Time”: time, “Notes”: notes]]. But when I present the data I want to present it by date, so after reading the data I was saving it to the following dictionary structure [Date: [“Engine Time”: time, [“Notes”: notes]]

Attached is a link to the screenshots:https://www.dropbox.com/sh/nuiuo3tuskh2c5m/AABdERgmlDa5go2MJYvnhsY4a?dl=0

Essentially, I want to know how do I present the data in a dictionary in a collection view cell.

Again thanks for your help

and why did you save it as [Date: [“Engine Time”: time, [“Notes”: notes]]?

your dictionary should be the same as your structure [“Date”: date, “Engine Time”: time, “Notes”: notes]]

Sorry Francis, but I am not sure I follow your question. The structure is that the date is the key and the value is an array containing engine time and notes. [Date: [String]]

I saved it under the key of date and an array engine time and notes.

I must be misunderstanding the structure syntax.

writing it like [Date: [“Engine Time”: time, [“Notes”: notes]] makes the “Date” key an actual word called “Date” not as a 01/22/2020 kind of date

perhaps check if this is what really happened in your case?

Sorry Francis I have been away. I will check this when I get back home. I should mention that when I populated the dictionary I made the key the actual date from the database. I didn’t have problems with the dictionary itself as I was able to print it. My problem was the line of code that put it into the collectionview cell:

cell.historyLabel.text = keys[indexPath.row]

Cannot subscript a value of type ‘Dictionary<Date, [String : Any]>.Keys’ with an argument of type ‘Int’

its because your result (keys[indexPath.row]) is an Int. it should be a String when you assign it to .text

so just simply add keys[indexPath.row].toString()

Thanks Francis, I have resolved this problem.

What did you do to solve it? Write it here and mark it as the solution to help someone else :slight_smile:

To solve the problem I created an array out of the dictionary and then used the array in the index path.