maymaymaymaymay
maymaymaymaymay

Reputation: 31

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?

Upvotes: 0

Views: 315

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 51983

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")

Upvotes: 1

Related Questions