Paul Paul - 7 months ago 18
Swift Question

swift binary operator cannot be applied to operands

I am begging to learn swift and I bought an app on the appStore called CODESWIFT for $5. I thought it'd be a nice easy way to start with the language, get familiar with the new ways of naming things and so on... On one of the exercises, the app has you creating these few variables and combining them to print to the console:

var company = "Apple"

let yearFounded = 1976

var str = "was founded in "

print(company + str + yearFounded)


When I do it on the app, it works (the app doesn't compile obviously, but it checks your code), but I decided to run the same exercise on Xcode and it comes back with an error:

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


This seems perfectly logic, but I guess I wasn't expecting the app to be a con. Have I been robbed of $5??

Answer

That's definitely not how to do it. These all work:

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