在Swift中连接字符串和整数

时间:2015-07-04 11:08:07

标签: swift concatenation

我希望不同类型之间的连接在Swift中是直截了当的,但显然不是。解决方法是什么?

var boy = "Bart Simpson"
var age = 7
print(boy + " is " + age)

2 个答案:

答案 0 :(得分:13)

非常简单,将所有可打印对象包裹在\( )

let boy = "Bart Simpson"
let age = 7
println("\(boy) is \(age)")

答案 1 :(得分:3)

let boy = "Bart Simpson"
let age = 7

let sentence = String(format:"%@ is %d", boy, age)
println(sentence) // "Bart Simpson is 7"