gpt4 book ai didi

ios - 无法使用类型 'String' 的参数列表调用类型 '(RemoteConfigValue)' 的初始值设定项

转载 作者:行者123 更新时间:2023-11-29 05:53:38 25 4
gpt4 key购买 nike

我正在尝试在 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")

}
}

搜索这个错误后,找到的正常解决方案类似these ones

但即使在应用这些解决方案之后,例如:


if let cityName = cities![i]["cityName"]! as? String {
print(cityName)
}

我仍然收到类似从“RemoteConfigValue”转换为不相关类型“String”总是失败的错误

如何解决这个问题?

最佳答案

请阅读documentation

class RemoteConfigValue : NSObject, NSCopying

This 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)
}
}
}

关于ios - 无法使用类型 'String' 的参数列表调用类型 '(RemoteConfigValue)' 的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com