Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将字典保存到swift中的Firebase?_Swift_Firebase_Dictionary_Firebase Realtime Database - Fatal编程技术网

如何将字典保存到swift中的Firebase?

如何将字典保存到swift中的Firebase?,swift,firebase,dictionary,firebase-realtime-database,Swift,Firebase,Dictionary,Firebase Realtime Database,我有一本[Int:Int]类型的字典,我正试图保存到firebase。我用于写入firebase的函数如下所示: func writeToFirebase() { setScoreToBadge() setScoreToVehicle() if GameManager.instance.getUsername() != "" { self.ref?.child("user").child( GameManager.insta

我有一本[Int:Int]类型的字典,我正试图保存到firebase。我用于写入firebase的函数如下所示:

func writeToFirebase() {
    setScoreToBadge()
    setScoreToVehicle()
    if GameManager.instance.getUsername() != "" {
        self.ref?.child("user").child(
                GameManager.instance.getUsername()
        ).setValue(
            [
                "email": GameManager.instance.getEmail(),
                 "userNameLowered": GameManager.instance.getUsername().lowercased(),
                 "userName": GameManager.instance.getUsername(),
                 "topScore": GameManager.instance.getTopScores()[0],
                 "topScores": GameManager.instance.getTopScores(),
                 "totalCash": GameManager.instance.getTotalCash(),
                 "friends": GameManager.instance.getFriendsAdded(),
                 "achievements": GameManager.instance.getAchievementsCompletedBool(),
                 "badge": GameManager.instance.getBadgeLevel(),
                 "scoreBadgeDictionary" : GameManager.instance.getTopScoreWithBadgeDictionary(),
                 "scoreVehicleDictionary" : GameManager.instance.getTopScoreWithVehicleDictionary()
            ])
    } else {
        print ("Not signed in, score not saved to firebase")
    }
}
我遇到的问题是
scoreBadgeDictionary
,ScoreVehicleDictionary
GameManager.instance.getTopScoreWithBadgeDictionary()
是类型为
[Int:Int]
GameManager.instance.getTopScoreWithVehicleDictionary()
的字典。如果我尝试运行此函数,它将崩溃


有没有办法将字典转换为可以保存到firebase的格式?

这个问题是如何定义数组的。一个简单的例子

let myArray = ["some_key": "some_value"]
定义为[String:String]的数组

定义为[String:Int]的数组

你的阵列是

[
"email": GameManager.instance.getEmail(),
"userNameLowered": GameManager.instance.getUsername().lowercased(),
.
.
"scoreBadgeDictionary" : ["some key": "some value"]
]
其中,我假设GameManager.instance.getTopScoreWithBadgeDictionary()返回一个字典

因此该数组不符合[String:String]或[String:Int]。但是,它确实符合更通用的[String:Any],因此这就是您需要“告诉”var的内容

let myArray: [String: Any] = [
     "email": "Hello",
     "userNameLowered": "hello",
     "scoreBadgeDictionary" : [1: "some score"],
     "scoreVehicleDictionary" : [2: "some vehicle"]
]
然而,这是重要的一点,在
getTopScoreWithVehicleDictionary
中返回的字典是[Int:String]或[Int:Int]不能用于Firebase

Firebase键必须是字符串,因此尝试编写此[1:“Hello,World]将崩溃。请注意,Firebase中的数组具有数字索引和

如果数据看起来像数组,Firebase会将其渲染为数组


我建议重新考虑该结构-可能将Int转换为字符串是一个选项,但这可能不适用于您的用例。

如果出现崩溃,请编辑您的问题,以包括确切的错误消息和堆栈跟踪。简短的回答是Firebase键必须是字符串,Firebase中的数组具有nu除外meric索引。也就是说,带数字键的字典的用例是什么?
let myArray: [String: Any] = [
     "email": "Hello",
     "userNameLowered": "hello",
     "scoreBadgeDictionary" : [1: "some score"],
     "scoreVehicleDictionary" : [2: "some vehicle"]
]