2016-04-23 41 views
-1

我要求學習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美元嗎?

+3

只有該應用程序的製造商可以告訴它爲什麼接受顯然是錯誤的代碼... –

+0

免費和*優秀*學習斯威夫特的來源:https://itunes.apple.com/us/book-series/swift-programming-series/id888896989?mt = 11 – Moritz

回答

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" 
0

您需要的Int值轉換爲String 試試這個:

print(company + str + "\(yearFounded)") 
相關問題