Swift : Writing ESCAPED Characters

Ok… this is a bit confusing, hopefully I can describe the situation clearly.

I have a SWIFT App that reads a TEXT file and then from that writes a SWIFT project file

The Input file may contain phrases the contain characters that SWIFT has to handle internally as escaped

Those are

  • Double Quote … /"
  • Single Quote … /’
  • Slash … //

So when this text is read in, the app converts the string so Swift can deal with it internally

s="This \ is a 'String'"

interally is

s="This \\ is a \'String\'"

However, I need to further escape things so when it is written out, the result can be compiled properly

When SWIFT writes the escaped string (2nd example) back to a text file, the result is the 1st Example

So I need to take and make a 3rd Example that appears in the text file like then 2nd, so when it compiles it comes out as the first string

confused? me too.

breaking down to a super simple example

Text Input : a="\"

what this app needs to WRITE is

Output : a="\\"

but again internally Swift already has it as

var s = "a=\"\\\\"

If I split that into single character array I get

V=["\"", "\\", "\""]

it is the middle \ that needs to somehow be changed

if v[1]=="\\" then v[1]=?????

??? can’t be /// or any other odd value

The answer turned out to be quite simple actually

In the output file, just add # to the string

var s = #"a="\""#

Still confused. What is the # character’s purpose? Any documentation you could point us to?

Swift, like many other languages, supports different kinds of string literals for different purposes


the # is an extended string delimiter