我正在使用Swift制作应用程序,并且正在使用Firebase Firestore。 Firestore是一个数据库,其中包含一些我放入UILabel中的字符串。带着一些刺痛,我正在使用换行命令(或\n)。所以我的一些字符串看起来像这样:

"This is line one\nThis is line two\nThis is line three"

但是,只要检索到该字符串,它就会添加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)!
   }
}

最佳答案

我知道了。我只是用换行命令替换了我收到的字符串中的字符“\ n”。

label.text = stringRecived.replacingOccurrences(of: "\n", with: "\n")

因为我手动输入了字符串并给Firebase像"Line one\nline two\nline three"我将“\ n”替换为“\ n”,但是如果给Firebase一个字符串,例如
"Line one
Line two
Line three"

Firebase在编写代码之前用"\\n"替换了这些返回值
label.text = stringRecived.replacingOccurrences(of: "\\n", with: "\n")

希望有帮助!

关于swift - 新行命令(\n)无法与Firebase Firestore数据库字符串一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48755746/

10-10 20:11