Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid escape sequence in literal Swift 3.0 [duplicate]

I have tried to print it but it just by passes because it's an escaped character. e.g output should be as follows.

\correct
like image 460
Rahul Sonvane Avatar asked Nov 18 '22 23:11

Rahul Sonvane


2 Answers

For that and also future reference:

\0 – Null character (that is a zero after the slash)
\\ – Backslash itself.  Since the backslash is used to escape other characters, it needs a special escape to actually print itself.
\t  – Horizontal tab
\n – Line Feed
\r  – Carriage Return
\”  – Double quote.  Since the quotes denote a String literal, this is necessary if you actually want to print one.
\’  – Single Quote.  Similar reason to above.
like image 159
C-JARP Avatar answered Dec 06 '22 05:12

C-JARP


Use the following code for Swift 5, Xcode 10.2

let myText = #"This is a Backslash: \"#
print(myText)

Output:

This is a Backslash: \

Now not required to add a double slash to use a single slash in swift 5, even now required slash before some character, for example, single quote, double quote etc.

See this post for latest update about swift 5

https://www.hackingwithswift.com/articles/126/whats-new-in-swift-5-0

like image 39
AtulParmar Avatar answered Dec 06 '22 03:12

AtulParmar