从数据库的字符串中删除/ n

时间:2017-11-07 04:39:18

标签: swift firebase firebase-realtime-database

我发现我的战士ID无法从数据库加载,因为我的路径不正确。这是因为字符串的末尾有一个/ n,我的数据库显然没有。这是发生错误的代码。

 func bronzePlayer () {
    loading.isHidden = false
    coverPacked.isHidden = false
    playerPackedCard.isHidden = false
    coverPackedCard.isHidden = false
    ref.child("Fighters/Bronze").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        self.packToNumber = value?["Total"] as! Int
        self.packedNumber = Int(arc4random_uniform(UInt32(self.packToNumber))) + 1
        self.setPackedString()

    }) { (error) in
        print(error.localizedDescription)
    }
    ref.child("\(packedNumberString)").observeSingleEvent(of: .value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary
        self.playerPackedID = value?["ID"] as! Int
        self.playerPackedPicture = value?["Picture"] as? String ?? ""

        self.loading.isHidden = true

    }) { (error) in
        print(error.localizedDescription)
    }
    playerPackedCard.texture = SKTexture(imageNamed: "\(playerPackedPicture)")
}

错误发生在

的位置
  self.playerPackedID = value?["ID"] as! Int

这是因为它找不到

  ref.child("\(packedNumberString)")

我需要在结束时摆脱/ n。有关如何的任何想法?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用filter删除字符串中的字符:

let filteredPackedNumberString = String(packedNumberString.characters.filter { !" \n".characters.contains($0) })

然后您可以将过滤后的字符串用作child parameter

ref.child("\(filteredPackedNumberString)")