swift二元运算符不能应用于操作数

时间:2016-04-23 19:14:58

标签: swift operand binary-operators

我乞求学习swift,我在appStore上买了一个名为CODESWIFT的应用程序,价格为5美元。我认为这是一个很好的简单方法,从语言开始,熟悉命名事物的新方法等等...在其中一个练习中,应用程序让你创建这些变量并将它们组合起来打印到控制台:

var company = "Apple"

let yearFounded = 1976

var str = "was founded in "

print(company + str + yearFounded)

当我在应用程序上执行此操作时(该应用程序显然无法编译,但它会检查您的代码),但我决定在Xcode上运行相同的练习并返回错误:< / p>

"binary operator '+' cannot be applied to operands of type 'String' and 'Int' 

这似乎是完全合乎逻辑的,但我想我并不期待该应用程序成为骗局。我被抢了5美元吗?

2 个答案:

答案 0 :(得分:1)

那绝对不是怎么做的。这些都有效:

var company = "Apple"
let yearFounded = 1976
var str = "was founded in"

print("\(company) \(str) \(yearFounded)")
print(company + " " + str + " " + String(yearFounded))
print(company, str, yearFounded)

// three times "Apple was founded in 1976"

答案 1 :(得分:0)

您需要将Int值转换为String 试试这个:

print(company + str + "\(yearFounded)")
相关问题