如何从 iOS swift 中的 Firebase 远程配置获取整数值?
how to get integer value from Firebase Remote Config in iOS swift?
所以我在我的 iOS 中设置了 Firebase 远程配置默认值,如下所示:
let remoteConfig = RemoteConfig.remoteConfig()
// set remote config default value
let defaultRemoteConfig : [String:NSObject] = [
"number_of_recommended_events_to_show_per_page" : 15 as NSObject
]
remoteConfig.setDefaults(defaultRemoteConfig)
// Activate and refetch remote config data.
// I use 'Load Value for next time' loading strategy
remoteConfig.activate()
remoteConfig.fetch()
然后我想像这样从远程获取值
// get the value from remote config
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue as! Int
我需要整数格式的值,但是当我像这样将它转换为 Int
时它崩溃了
这是我在控制台中设置值的方法
为什么是零?如何解决这个问题?
try this!
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue?.intValue ?? 0
确保在获取远程值之前已在此块中获取数据。
func fetchCloudValues() {
// WARNING: Don't actually do this in production!
let fetchDuration: TimeInterval = 0
RemoteConfig.remoteConfig().fetch(withExpirationDuration: fetchDuration) { [weak self] status, error in
if let error = error {
print ("Uh-oh. Got an error fetching remote values \(error)")
return
}
RemoteConfig.remoteConfig().activateFetched()
print ("Retrieved values from the cloud!")
let numberOfEvents = RemoteConfig.remoteConfig()
.configValue(forKey: "number_of_recommended_events_to_show_per_page")
.intValue ?? 0
print("Our app's number of events is \(numberOfEvents)")
}
}
所以我在我的 iOS 中设置了 Firebase 远程配置默认值,如下所示:
let remoteConfig = RemoteConfig.remoteConfig()
// set remote config default value
let defaultRemoteConfig : [String:NSObject] = [
"number_of_recommended_events_to_show_per_page" : 15 as NSObject
]
remoteConfig.setDefaults(defaultRemoteConfig)
// Activate and refetch remote config data.
// I use 'Load Value for next time' loading strategy
remoteConfig.activate()
remoteConfig.fetch()
然后我想像这样从远程获取值
// get the value from remote config
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue as! Int
我需要整数格式的值,但是当我像这样将它转换为 Int
时它崩溃了
这是我在控制台中设置值的方法
为什么是零?如何解决这个问题?
try this!
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue?.intValue ?? 0
确保在获取远程值之前已在此块中获取数据。
func fetchCloudValues() {
// WARNING: Don't actually do this in production!
let fetchDuration: TimeInterval = 0
RemoteConfig.remoteConfig().fetch(withExpirationDuration: fetchDuration) { [weak self] status, error in
if let error = error {
print ("Uh-oh. Got an error fetching remote values \(error)")
return
}
RemoteConfig.remoteConfig().activateFetched()
print ("Retrieved values from the cloud!")
let numberOfEvents = RemoteConfig.remoteConfig()
.configValue(forKey: "number_of_recommended_events_to_show_per_page")
.intValue ?? 0
print("Our app's number of events is \(numberOfEvents)")
}
}