如何从iOS Swift中的Firebase远程配置中获取整数值? [英] how to get integer value from Firebase Remote Config in iOS swift?
本文介绍了如何从iOS Swift中的Firebase远程配置中获取整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在iOS中将Firebase远程配置设置为默认值,
so I set Firebase remote config default in my iOS like this:
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()
然后我想像这样从远程获取值
and then I want to get the value from remote like this
// get the value from remote config
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue as! Int
我需要Integer格式的值,但是当我将其强制转换为 Int
时,它会崩溃
I need the value in Integer format, but it crash when I cast it to Int
like that
这是我在控制台中设置值的方式
here is how I set the value in the console
为什么没有?如何解决这个问题?
why is it nil ? how to fix this ?
推荐答案
在获取远程值之前,请确保已在此块中进行了获取.
Make sure you've fetched in this block before getting a remote value.
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 Swift中的Firebase远程配置中获取整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文