swift二元运算符不能应用于操作数
问题描述:
我要求学习swift,我以5美元的价格在名为CODESWIFT的appStore上购买了一个应用程序。我认为从语言开始,熟悉命名事物的新方法等等,这将是一个很好的简单方法......在其中一个练习中,应用程序将创建这些变量并将它们组合起来以打印控制台:swift二元运算符不能应用于操作数
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美元吗?
答
这绝对不是如何做到这一点。这些所有的工作:
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"
答
您需要的Int
值转换为String
试试这个:
print(company + str + "\(yearFounded)")
只有该应用程序的制造商可以告诉它为什么接受显然是错误的代码... –
免费和*优秀*学习斯威夫特的来源:https://itunes.apple.com/us/book-series/swift-programming-series/id888896989?mt = 11 – Moritz