新行命令(\n)不使用Firebase Firestore数据库字符串

Jac*_*vin 4 string newline firebase swift google-cloud-firestore

我正在使用Swift制作应用程序,而我正在使用Firebase Firestore.Firestore是一个数据库,它包含一些我放入的字符串UILabel.我的一些叮咬,我使用新的行命令(或\n).所以我的一些字符串看起来像这样:

"This is line one\nThis is line two\nThis is line three"
Run Code Online (Sandbox Code Playgroud)

但是,无论何时检索到该字符串,它都是addetoto UILabel并且看起来像这样......

这是第一行\n这是第二行\n这是第三行

......应该是这样......

这是第一行

这是第二行

这是第三行

我假设这\n不适用于来自数据库的字符串?我试过双重逃避\\n.有人有解决方法吗?

这是我正在使用的代码......

    database.collection("songs").whereField("storyTitle", isEqualTo: "This is the story header").getDocuments { (snapshot, error) in

        for document in (snapshot?.documents)! {
            self.storyBodyLabel.text = (document.data()["storyBody"] as? String)!
   }
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*vin 9

我知道了.我只是用newline命令从我收到的字符串中替换了字符"\n".

label.text = stringRecived.replacingOccurrences(of: "\n", with: "\n")
Run Code Online (Sandbox Code Playgroud)

因为我手动输入我的字符串并给Firebase一个刺痛,就像 "Line one\nline two\nline three"我用"\n"替换"\n"但是如果你给Firebase一个字符串就像

"Line one
Line two
Line three"
Run Code Online (Sandbox Code Playgroud)

Firebase会在"\\n"制作代码时替换这些退货

label.text = stringRecived.replacingOccurrences(of: "\\n", with: "\n")
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 你确定要更换\n吗?Firebase将\n转换为\\n,因此您需要使用stringRecived.replacingOccurrences(of:"\\n",使用:"\n")转换回来 (8认同)