I'm relatively new with Firebase's new Cloud Firestore and I'm having difficulty trying to map data to/from. I've tried following documentation via Google online, but it has some issues I cannot figure out the origin of.
[String : Any] to my custom struct, the documentation suggested I try the following:docRef.getDocument { (document, error) in
let result = Result {
try document.flatMap {
try $0.data(as: City.self)
}
}
switch result {
case .success(let city):
if let city = city {
print("City: \(city)")
} else {
print("Document does not exist")
}
case .failure(let error):
print("Error decoding city: \(error)")
}
}
However, this produced an error on the line $0.data(as: City.self) with Value of type 'NSObject' has no member 'data'.
do {
try db.collection("cities").document("LA").setData(from: city)
} catch let error {
print("Error writing city to Firestore: \(error)")
}
But this also produces an error on the .setData(from: city) with Argument labels '(from:)' do not match any available overloads.
Does anyone have any familiarity with this to try to provide additional clarity for casting Firestore data to custom structs? I understand my structs are intended to be codable.
If I understand correctly, you are having the same issue I had a few days ago: Firebase (Cloud Firestore) - How to convert document to a custom object in Swift 5?
The key here is to do import FirebaseFirestoreSwift explicitly instead of just doing import Firestore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With