给定一个字符串:"My name is Gustavo"
,它是用 "My name is\(foo.name)"
创建的
是否有可能在值被替换后得到字符串"My name is\(foo.name)"
?
换句话说,是否有可能找出 "My name is\(foo.name)"
,"My name is Gustavo"
?
编辑:如果我能找到“我的名字是”
,也就是动态构建的所有字符串中的硬编码字符串,那将是一个解决方案。
最佳答案
不,这是不可能的。事实上,"My name is\(foo.name)"
甚至没有包含在编译代码中。相反,Swift-5 编译器生成与此等效的代码:
let str = String(stringInterpolation: {
var temp = String.StringInterpolation(literalCapacity: 11, interpolationCount: 1)
temp.appendLiteral("My name is ")
temp.appendInterpolation(foo.name)
return temp
}())
This article包含有关如何在 Swift-4 和 Swift-5 中实现字符串插值的详细信息。
关于swift - 值替换后恢复字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56586857/