[英]String literal in Swift - new line character \n not recognized
I want to declare a string that contains my public RSA key. 我想声明一个包含我的公共RSA密钥的字符串。 In Objective-C I did it like this:
在Objective-C中,我这样做的是:
NSString *pubKey = @"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu\n"
"ERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A\n"
"+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw\n"
"mwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN\n"
"QSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P\n"
"+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB\n"
"-----END RSA PUBLIC KEY-----";
When I display this string in the debugger, it looks like this: 当我在调试器中显示此字符串时,它看起来像这样:
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu
ERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A
+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw
mwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN
QSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P
+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB
-----END RSA PUBLIC KEY-----
I am now trying to declare the exact same string in Swift (Xcode 10.2) and have not been able to get it to work. 我现在正尝试在Swift(Xcode 10.2)中声明完全相同的字符串,但无法使其正常工作。
What I tried was this: 我试过的是:
let pubKeyArray : [String]
let pubKey : String
self.pubKeyArray = ["-----BEGIN RSA PUBLIC KEY-----",
"MIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu",
"ERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A",
"+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw",
"mwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN",
"QSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P",
"+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB",
"-----END RSA PUBLIC KEY-----"]
self.pubKey = self.pubKeyArray.joined(separator: "\n")
When I display this string in the Debugger, it looks like this: 当我在调试器中显示此字符串时,它看起来像这样:
"-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu\nERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A\n+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw\nmwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN\nQSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P\n+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB\n-----END RSA PUBLIC KEY-----"
So basically, the new line character is not recognized. 因此,基本上,新行字符无法识别。 I also tried "\\\\n" and "\\r\\n", but to no avail.
我也尝试过“ \\\\ n”和“ \\ r \\ n”,但无济于事。
What is the correct separator to use here? 在这里使用正确的分隔符是什么?
The string you are seeing in the debugger is the debugDescription
of the string. 您在调试器中看到的字符串是该字符串的
debugDescription
。 The debugDescription
will replace things like the new line character with a backslash + n
(It is supposed to make it clear that there is a new line). debugDescription
将用反斜杠+ n
替换新行字符之类的东西(应该清楚地表明有新行)。
If you want to see the actual string, you should just print
it. 如果您想查看实际的字符串,则只需
print
它即可。 In lldb
, do: 在
lldb
,执行以下操作:
po print(pubKey)
Rather than building an array and building a string from that, you can instead use a multi-line string literal. 您可以使用多行字符串文字,而不是构建数组并从中构建字符串。 As The Swift Programming Language says:
正如Swift编程语言所说:
Multiline String Literals
多行字符串文字
If you need a string that spans several lines, use a multiline string literal—a sequence of characters surrounded by three double quotation marks:
如果您需要一个跨越多行的字符串,请使用多行字符串文字-由三个双引号引起来的字符序列:
let quotation = """ The White Rabbit put on his spectacles. "Where shall I begin, please your Majesty?" he asked. "Begin at the beginning," the King said gravely, "and go on till you come to the end; then stop." """
So, in your case: 因此,在您的情况下:
let pubKey = """
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu
ERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A
+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw
mwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN
QSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P
+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB
-----END RSA PUBLIC KEY-----
"""
Note that blank line between “END RSA PUBLIC KEY” line and the closing quotes. 请注意,“ END RSA PUBLIC KEY”行和右引号之间的空白行。 That's because your original Objective-C example suggested that you wanted a newline at the end of that last line.
这是因为您最初的Objective-C示例建议您在最后一行的末尾添加换行符。 If you don't need that last newline character, just omit that blank line at the end, eg:
如果不需要最后一个换行符,只需在末尾省略该空白行即可,例如:
let pubKey = """
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA+xGZ/wcz9ugFpP07Nspo6U17l0YhFiFpxxU4pTk3Lifz9R3zsIsu
ERwta7+fWIfxOo208ett/jhskiVodSEt3QBGh4XBipyWopKwZ93HHaDVZAALi/2A
+xTBtWdEo7XGUujKDvC2/aZKukfjpOiUI8AhLAfjmlcD/UZ1QPh0mHsglRNCmpCw
mwSXA9VNmhz+PiB+Dml4WWnKW/VHo2ujTXxq7+efMU4H2fny3Se3KYOsFPFGZ1TN
QSYlFuShWrHPtiLmUdPoP6CV2mML1tk+l7DIIqXrQhLUKDACeM5roMx0kLhUWB8P
+0uj1CNlNN4JRZlC7xFfqiMbFRU9Z4N6YwIDAQAB
-----END RSA PUBLIC KEY-----
"""
Now, when you display this in the debugger, you'll see the \\n
characters represented there, but that's just an artifact of how the debugger represents non-visible characters. 现在,当在调试器中显示该字符时,您会看到
\\n
字符,但这只是调试器如何表示不可见字符的产物。 Rest assured that those are really newline characters. 请放心,这些确实是换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.