我想在字符串中添加一些变量:
var age:Int
var pets:String
lblOutput.text = "Your"+ var pets +"is"+ var age +"years old!"
这两个变量都不是nil。我想这就是objective-c中的工作方式,不是吗?
谢谢!
\()
来完成的。就像这样:let x = 10
let string = "x equals \(x) and you can also put expressions here \(5*2)"
所以针对你的例子,做如下操作:
var age:Int=1
var pet:String="dog"
lblOutput.text = "Your \(pet) is \(age) years old!"
let args = [pets, age]
let msg = String(format: "Your %@ is %@ years old", arguments: args)
print(msg)
例子:
var age = 27
var name = "George"
print("I'm \(name), My age is \(age)")
output: I'm George, My age is 27
你需要在(age)前面添加反斜杠