-1

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

var company = "Apple"

let yearFounded = 1976

var str = "was founded in "

print(company + str + yearFounded)

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

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

这似乎完全合乎逻辑,但我想我没想到该应用程序会成为骗局。我被抢了5美元吗?

4

2 回答 2

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"
于 2016-04-23T19:20:31.000 回答
0

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

print(company + str + "\(yearFounded)")
于 2016-04-23T19:24:50.633 回答