从文件中解析Swift String插值不起作用

时间:2019-06-10 10:46:10

标签: swift string-interpolation

我正在使用插值对字符串进行编码:

  

My name is John

     

到文件

运行代码时,我需要从文件中解码该字符串,我希望该字符串显示My name is \(name),但我得到的只是class Book < ApplicationRecord belongs_to :category, required: false belongs_to :subcategory, required: false def self.search(keywords) if keywords where('name LIKE ? OR description LIKE ? OR author LIKE ? OR abstract LIKE ?', "%#{keywords}%", "%#{keywords}%", "%#{keywords}%", "%#{keywords}%").order('id DESC') else order('id DESC') end end end

在这种情况下,有没有办法让Swift理解字符串插值?

1 个答案:

答案 0 :(得分:1)

更好的解决方案是使用String(format:)并在解析时使用您想要的任何变量

将“我的名字是%s”写到文件中,然后在阅读时

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