Firebase:从Int实时数据库中检索数据

时间:2022-08-08 13:18:29

i've been using firebase to retrieve the test data from the firebase database and set the value to a label but the problem i am having is that i want to retrieve the data as an Int instead of the String.

我一直在使用firebase从firebase数据库中检索测试数据并将值设置为标签,但我遇到的问题是我想要将数据检索为Int而不是String。

  func retData(){
    rootRef.child("users").child("Test").observeEventType(.Value){
        (snap: FIRDataSnapshot) in
        self.simpleLabel.text = snap.value?.description

    }    }

1 个解决方案

#1


1  

Firebase represents data as JSON strings, and can only be retrieved as such. I ran into a similar issue on a recent project & had to resort to int>string for pushing data to FB, and string>int on the string retrieved from FB.No code submitted for the question, but basically you have to cast the int to a string using constructor syntax as:

Firebase将数据表示为JSON字符串,并且只能这样检索。我在最近的一个项目中遇到了类似的问题,并且不得不求助于int> string将数据推送到FB,并且从FB.No代码中检索到的字符串上的字符串> int,但是基本上你必须转换int使用构造函数语法的字符串:

let myStr = String(myInt) //push the string up to Firebase

让myStr = String(myInt)//将字符串推送到Firebase

......

//pull the string back from Firebase

//从Firebase拉回字符串

let myInt =Int(myStr)

让myInt = Int(myStr)

#1


1  

Firebase represents data as JSON strings, and can only be retrieved as such. I ran into a similar issue on a recent project & had to resort to int>string for pushing data to FB, and string>int on the string retrieved from FB.No code submitted for the question, but basically you have to cast the int to a string using constructor syntax as:

Firebase将数据表示为JSON字符串,并且只能这样检索。我在最近的一个项目中遇到了类似的问题,并且不得不求助于int> string将数据推送到FB,并且从FB.No代码中检索到的字符串上的字符串> int,但是基本上你必须转换int使用构造函数语法的字符串:

let myStr = String(myInt) //push the string up to Firebase

让myStr = String(myInt)//将字符串推送到Firebase

......

//pull the string back from Firebase

//从Firebase拉回字符串

let myInt =Int(myStr)

让myInt = Int(myStr)