在Swift中将整数保存到Firebase数据库,不需要使用引号。

huangapple go评论82阅读模式
英文:

Save integer without quotations into firebase database in swift

问题

我正在尝试使用setValue将一个数字放入Firebase数据库,但当我放入时,它总是变成一个字符串。示例:"10"而不是10。我已经仔细检查了我要设置的值实际上是一个整数或NSNumber。但都不起作用。

var modeResId = (self.modes[self.currModeNum].modeId as NSNumber)  
ref.child(Mode.Parameter.MODE_ID).setValue(modeResId)
英文:

I am trying to put a number into firebase database with setValue, but it always changes to a string when I put it in. Example: "10" instead of 10. I have double checked that the value which I am setting is actually an int or NSNumber. None of which works.

var modeResId = (self.modes[self.currModeNum].modeId as NSNumber)  
ref.child(Mode.Parameter.MODE_ID).setValue(modeResId)

答案1

得分: 1

modeId 先转换为 Int,然后再将其 setValue 到 Firebase。

英文:

Hope this helps:

var modeResId = (self.modes[self.currModeNum].modeId as NSNumber)  
ref.child(Mode.Parameter.MODE_ID).setValue(Int(modeResId) ?? 0) //If parsing fails then setValue 0

Convert modeId into an Int first and then setValue into firebase.

答案2

得分: 0

问题在于我看错了键值。现在它正常工作。

英文:

The problem was that I was looking at the wrong key value. Now it works.

huangapple
  • 本文由 发表于 2020年1月7日 01:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定