gpt4 book ai didi

swift - 换行命令 (\n) 不适用于 Firebase Firestore 数据库字符串

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:10 27 4
gpt4 key购买 nike

我正在使用 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 一个像这样的字符串“第一行\n第二行\n第三行”我将“\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/

27 4 0
文章推荐: ios - 在swift4中查找按钮的文本颜色
文章推荐: html - 如何 float 不同高度的div?
文章推荐: ios - 即使已取消并设置为Nil,Swift iOS -DispatchWorkItem仍在运行
文章推荐: html - 为什么 不起作用?