I am writing a function that allows me to read the data inside the Database. I can read the String from Firebase but I cannot read the arrays. I tried like this but it doesn't work:
func fetchData(collection: String) {
DatabaseFirestore.collection(collection).addSnapshotListener { (querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("No documents")
return
}
self.example = documents.map { (queryDocumentSnapshot) -> Example in
let data = queryDocumentSnapshot.data()
let ex1 = data["ex1"] as? String ?? "" // it is not an array in the database
let ex2 = data["ex2"] as? String ?? "" // is an array in the database
return Example(ex1: ex1, ex2: ex2)
}
}
}
How could I change this line so that it reads the array inside the database?
let ex2 = data["ex2"] as? String ?? ""
as? [Any]instead ofas? String? But it's unclear what you need exactly since you are mixing Array & String.Stringand an array of String,[String], then it's time to read the basics again at: docs.swift.org/swift-book/LanguageGuide/TheBasics.html