在學(xué)Swift基本類型的時候發(fā)現(xiàn)在String Interpolation這部分有這樣一段話
print(#"6 times 7 is \#(6 * 7)."#)
// Prints "6 times 7 is 42."
NOTE
The expressions you write inside parentheses within an interpolated string can’t contain an unescaped backslash (), a carriage return, or a line feed. However, they can contain other string literals.
在Note中有兩個概念即"carriage return"和"line feed"堡赔,分別代表\r和\n字符
在stackoverflow上的討論是這樣講的
A line feed means moving one line forward. The code is \n.
A carriage return means moving the cursor to the beginning of the line. The code is \r
\n和\r在ASCII表中的值分別是0x0A和0x0D
從stackoverflow上的回答可以看出在windows平臺換行的字符是"\r\n"休建,在類UNIX平臺代表換行的字符是"\n"
這里的換行可以形象在理解為除光標(biāo)之外現(xiàn)有所有字符向上移動一行,同時光標(biāo)回到行首,回到打字機的時代,就比較好理解這個概念了,因為這其實是兩步,一步是紙張上移一行,一步是光標(biāo)回行首
所以Note中的意思是如果字符串中包含了carriage return卦尊,或者line feed 是不被允許的行為,比如在行尾敲擊回車鍵的話舌厨,XCode提示如下:
這里其實又衍生出另外一個問題岂却,敲擊回車鍵之后,觸發(fā)的字符究竟是\r還是\n還是\r\n呢?
hexdump -x 文件
得到第十二個位置的字符utf8編碼為0a即line feed的ascii編碼(二進制展示使用的是小端編碼扫尺,所以第11個位置的0a即第12個字符的編碼筋栋,第12個編碼0x68是字符h的編碼)