swift - 值替换后恢复字符串插值

标签 swift string string-interpolation

给定一个字符串:"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/

相关文章:

javascript - 无法解析数组中的字符串化数组

python - 有没有办法在 Python 的多行字符串中使用变量?

swift - 我在 Swift 4 中收到如下错误 :

Swift:与 where 约束关联的类型

java - 在 Java 中重用具有常量后缀的字符串的最佳实践

swift 4 : modifying numbers in String

scala - 具有最大位数的字符串格式

ios - 访问 Cocoa 框架 Storyboard

swift - iOS : A specific function can be called from multiple threads/places, 但我希望每次调用都在队列中执行

python - 在 Python 中连接字符串的首选方法是哪种?