如何从iOS Swift中的Firebase远程配置中获取整数值?

编程入门 行业动态 更新时间:2024-10-05 09:27:33
本文介绍了如何从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远程配置中获取整数值?

本文发布于:2023-11-26 23:14:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   iOS   Swift   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!