Я пытаюсь получить строку (внутри объекта) в Firebase (используя Swift)
let currentDocument = db.collection("countries").document("United States")
currentDocument.getDocument { (document, error) in
if let document = document, document.exists {
let cities = document.data()!["cities"] as? [AnyObject] // This grabs data from a Firebase object named `cities`, inside the object there are arrays that have two pieces of data (e.g. ["cityName" : "New York", "currentTemperature" : 38])
for i in 0..<cities!.count {
let cityName = String(cities![i]["cityName"]!) // Here is where I get the error `Cannot invoke initializer for type 'String' with an argument list of type '(RemoteConfigValue)'`
}
} else {
print("Document does not exist")
}
}
После поиска этой ошибки нормальные решения, которые я нашел, похожи на Вот эти
Но даже после применения этих решений, например:
if let cityName = cities![i]["cityName"]! as? String {
print(cityName)
}
Я все еще получаю сообщение об ошибке, например Cast from 'RemoteConfigValue' to unrelated type 'String' always fails
Как мне это решить?





Пожалуйста, прочтите документация
class RemoteConfigValue : NSObject, NSCopyingThis class provides a wrapper for Remote Config parameter values, with methods to get parameter values as different data types.
Так что вам нужно написать что-то вроде этого
if let cities = document.data()!["cities"] as? [[String:Any]] { // cities is obviously an array of dictionaries
for city in cities { // don't use index based loops if you actually don't need the index
if let cityName = city["cityName"] as? RemoteConfigValue {
print(cityName.stringValue)
}
}
}
Это не сработало, но я не понимаю, почему? Логика кажется хорошей, и если я это сделаю print(city), она будет нормально напечатана... Но все остальное возвращает nil
Попробуй это
let currentDocument = db.collection("countries").document("United States")
currentDocument.getDocument { (document, error) in
if let document = document, document.exists {
let cities = document.data()!["cities"] // This grabs data from a Firebase object named `cities`, inside the object there are arrays that have two pieces of data (e.g. ["cityName" : "New York", "currentTemperature" : 38])
for i in 0..<cities!.count {
if let cityName = cities[i]["cityName"]! as? String {
print(cityName)
}
}
} else {
print("Document does not exist")
}
}
Во-первых, вам не нужно использовать RemoteConfigValue с кодом или задачей, представленной в вопросе. Во-вторых, приложите скриншот структуры вашего FireStore и более четкое объяснение того, что вы пытаетесь сделать. Пока мы этого не получим, ответы будут присылать вам все указания, которые могут быть неправильными для вашего варианта использования.