Search code examples
swiftstring-interpolation

Swift String interpolation not working when it's parsed from a file


I am encoding a string with interpolation:

let name = "John" "My name is \(name)"

to a file

And i need to decode that string from file when running my code, i am expecting the string to print My name is John, but all i get is My name is \(name)

Is there a way to let Swift understand string interpolation in this case?


Solution

  • A better solution could be to use String(format:) and use any variable you want when parsing

    Write "My name is %s" to file and then when reading it

    let inString = "My name is %s"  //from file really
    let str = String(format: inString, "John")