0

Is there a way to automatically escape characters in Swift?

var myString = someMagicWord("#%"$@\/")

In c#, someMagicWord = @

In Android studio you can paste your weird string and the ide will automatically escape charecters for you.

Adding @zaph's example: There is a file with characters such as newline "\n", tab "\t", etc. Open the file, select all, copy, go to Xcode and paste inside a string statement such as @"pasted_code_here". The OP wants these characters escaped automatically to something like: @"\n\t"

Luda
  • 7,282
  • 12
  • 79
  • 139
  • 3
    Im not sure I understand the question – Andrius Steponavičius Sep 07 '15 at 14:47
  • 3
    Example: There is a file with characters such as newline "\n", tab "\t", etc. Open the file, select all, copy, go to Xcode and paste inside a string statement such as @"pasted_code_here". The OP wants these characters escaped automatically to something like: @"\n\t". – zaph Sep 07 '15 at 15:35
  • Then the question should be probably tagged with `Xcode` since it has nothing to do with `ios` or `swift`... – Alladinian Oct 23 '15 at 15:00

2 Answers2

0

With Swift 5 you can use raw strings, like so

var myString = #"#%"$@\/"#
Frizlab
  • 846
  • 9
  • 30
-1

While in some languages this is doable (e.g.:python) in swift "escaping" those special characters is not really possible because newlines: \n, tablines : \t etc are string literals. You can have a look here about that

string literals

Korpel
  • 2,432
  • 20
  • 30