Firebase数据库仅加载字符串

时间:2017-11-04 06:55:39

标签: swift string firebase firebase-realtime-database

我在加载播放器ID时成功,但是当我使用该ID加载图片时,它会失败。因此,func loadPlayers可以工作,而func getPictures则不然。我不知道为什么,我做过这个是其他项目并且它有效。任何帮助表示赞赏!

 func loadPlayers () {
    ref = Database.database().reference()
    ref.child("Users/\(playerIDentered)").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        USfighter1.ID = value?["fighter1ID"] as! Int
        USfighter2.ID = value?["fighter2ID"] as! Int
        USfighter3.ID = value?["fighter3ID"] as! Int
        USfighter4.ID = value?["fighter4ID"] as! Int
        USfighter5.ID = value?["fighter5ID"] as! Int
        USfighter6.ID = value?["fighter6ID"] as! Int
        USfighter7.ID = value?["fighter7ID"] as! Int
        USfighter8.ID = value?["fighter8ID"] as! Int
        USfighter9.ID = value?["fighter9ID"] as! Int
        USfighter10.ID = value?["fighter10ID"] as! Int
        USfighter11.ID = value?["fighter11ID"] as! Int
        USfighter12.ID = value?["fighter12ID"] as! Int
        USfighter13.ID = value?["fighter13ID"] as! Int
        USfighter14.ID = value?["fighter14ID"] as! Int
        USfighter15.ID = value?["fighter15ID"] as! Int
        USfighter16.ID = value?["fighter16ID"] as! Int

    }) { (error) in
        print(error.localizedDescription)
    }

}

func getPictures () {
    //ref = Database.database().reference()
    ref.child("\(USfighter1.ID)").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        USfighter1.Picture = value?["Picture"] as? String ?? ""
        self.fighter1.texture = SKTexture(imageNamed: "\(USfighter1.Picture)")

    }) { (error) in
        print(error.localizedDescription)
    }

这也是数据库结构。

 {
   "1" : {
     "Fname" : "Sunny",
     "Lname" : "Liston",
     "Picture" : "90-SunnyListon"
   },
   "2" : {
     "Fname" : "Rocky",
     "Lname" : "Marciano",
     "Picture" : "94-RockyMarciano"
   },
   "Fighters" : [ null, {
     "Fname" : "Sunny",
     "Lname" : "Liston",
     "Picture" : "90-SunnyListon"
   }, {
     "Fname" : "Rocky",
     "Lname" : "Marciano",
     "Picture" : "94-RockyMarciano"
   } ],
   "Users" : [ null, {
     "Coins" : 10000000,
     "fighter10ID" : 0,
     "fighter11ID" : 0,
     "fighter12ID" : 0,
     "fighter13ID" : 0,
     "fighter14ID" : 0,
     "fighter15ID" : 0,
     "fighter16ID" : 0,
     "fighter1ID" : 2,
     "fighter2ID" : 1,
     "fighter3ID" : 0,
     "fighter4ID" : 0,
     "fighter5ID" : 0,
     "fighter6ID" : 0,
     "fighter7ID" : 0,
     "fighter8ID" : 0,
     "fighter9ID" : 0
   } ]
 }

0 个答案:

没有答案