-1

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??

Paul
  • 1,277
  • 5
  • 28
  • 56
  • 3
    Only the producers of that app can tell why it accepts obviously wrong code ... – Martin R Apr 23 '16 at 19:20
  • A free and *excellent* source for learning Swift: https://itunes.apple.com/us/book-series/swift-programming-series/id888896989?mt=11 – Eric Aya Apr 23 '16 at 19:27

2 Answers2

1

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"
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
0

You need to cast the Int value to String Try this:

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