如何解决如何在Swift中使非可选字符串成为可选
我正在将Stripe实施到我的应用程序中,因此当我快速发送我的customer_id作为参数时,它必须是可选的,否则将不起作用。但是我从firebase中提取了id作为非可选值。有什么办法可以将非可选ID转换为可选变量。从消防站获取ID的代码:
docRef.getDocument(source: .cache) { (document,error) in
if let document = document {
id = document.get("id") as? String ?? ""
}
id作为非可选返回。我需要发送一个可选变量作为id(这是它唯一的工作方式):
var data = ["api_version" : apiVersion,"customer_id" : id]
functions.httpsCallable("getStripeEphemeralKeys").call(data)
上面的代码给了我错误,但是如果我发送与可选变量相同的确切ID,它就会起作用
解决方法
我解决了这个问题。如果您执行document [“ id”}而不是document.get(“ id”),firebase如何返回可选参数真的很奇怪。
var id:Any? = ""
let docRef = db.collection("Profile").document((user?.email)!)
docRef.getDocument(source: .cache) { (document,error) in
if let document = document {
id = document["id"]
var data = ["api_version" : apiVersion,"customer_id" : id]
self.functions.httpsCallable("getStripeEphemeralKeys").call(data) {
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。