john doe john doe - 1 month ago 16
Objective-C Question

Adding back slash for quotes in Swift

I have the following code in Objective-C:

NSString *someString = @"Hello World";


I need a Swift Code with the following output:
\"Hello World\"


Including the double checkmarks and the backslash.

Answer

According to your comment, you'll need this:

var mySuperFancyDynamicString: String = "whaaaazzuppp"

let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)

print is \"whaaaazzuppp\" (well... was Hello World, before the edit)

enter image description here

Or to your "text box" Comment:

var mySuperFancyDynamicString: String = textBox.text
let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)

Print will be : \"textBoxContent\"