新行命令(\ n)无法与Firebase Firestore数据库字符串一起使用 [英] New Line Command (\n) Not Working With Firebase Firestore Database Strings

查看:89
本文介绍了新行命令(\ n)无法与Firebase Firestore数据库字符串一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making an app with Swift and I'm using Firebase Firestore. Firestore is a database that has some strings that I put into a UILabel. With some of my stings, I am using the new line command (or \n). So some of my strings look like this:

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

但是,每当检索到该字符串时,它就是UILabel的附加字符,并显示如下...

But, whenever that string is retrieved, it's addetoto the UILabel and appears like this...

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

...什么时候应该是这样...

...when it should be like this...

这是第一行

这是第二行

这是第三行

我假设\n不适用于来自数据库的字符串?我尝试使用\\n进行两次转义.有人对此有解决办法吗?

I'm assuming that \n does not work with strings coming from a database? I've tried double escaping with \\n. Does anyone have a fix for this?

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

Here is the code that I am using...

    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".

I got it. I simply just replaced the character "\n" from the string that I was receiving with the newline command.

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

因为我手动键入了我的字符串并给了Firebase一个类似的提示 "Line one\nline two\nline three"我将"\ n"替换为"\ n",但是如果您给Firebase输入一个像这样的字符串

Because I manually typed out my string and gave Firebase a sting like "Line one\nline two\nline three" I am replacing "\n" with "\n" But if you give Firebase a string like

"Line one
Line two
Line three"

Firebase在编写代码之前用"\\n"替换了这些返回值

Firebase replaces those returns with "\\n" therfore making the code

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

希望有帮助!

这篇关于新行命令(\ n)无法与Firebase Firestore数据库字符串一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆