18

Hi I have a question about this code:

1)

let label = "The width is "
let width = 94
let widthLabel = label + String(width)

2)

let height = "3"
let number = 4
let hieghtNumber = number + Int(height)

The first part is working just fine, but I don't get why the second one is not. I am getting the error 'Binary operator "+" cannot be applied to two int operands', which to me does not make much of sense. Can someone help me with some explanation?

Neli Chakarova
  • 660
  • 1
  • 5
  • 18
  • See also the answer to [this question](http://stackoverflow.com/questions/40557214/swift-operator-throwing-error-on-two-ints). Essentially, if A is already declared as a Double of CGFloat or whatever, and B and C are integers, A = B + C will fail with this error message, which obscures the real issue and solution: A = Double(B+C). This question/answer is at the top of the Google search for this error; in some cases the other question's answer may be more helpful. – ConfusionTowers Mar 24 '17 at 13:34

2 Answers2

18

1) The first code works because String has an init method that takes an Int. Then on the line

let widthLabel = label + String(width)

You're concatenating the strings, with the + operator, to create widthLabel.

2) Swift error messages can be quite misleading, the actual problem is Int doesn't have a init method that takes a String. In this situation you could use the toInt method on String. Here's an example:

if let h = height.toInt() {
    let heightNumber = number + h
}

You should use and if let statement to check the String can be converted to an Int since toInt will return nil if it fails; force unwrapping in this situation will crash your app. See the following example of what would happen if height wasn't convertible to an Int:

let height = "not a number"

if let h = height.toInt() {
    println(number + h)
} else {
    println("Height wasn't a number")
}

// Prints: Height wasn't a number

Swift 2.0 Update:

Int now has an initialiser which takes an String, making example 2 (see above):

if let h = Int(height) {
    let heightNumber = number + h
}
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Does it make any sense to use (Int)someString in any situation. I am just getting confused by the error description that I get that the operands are Int, which means the height is somehow casted to Int. – Neli Chakarova May 20 '15 at 12:37
  • 1
    No, firstly, that's not the syntax for casting in Swift (you would use a form of `as`, see https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html). Secondly, Swift error messages have a habit of being quite misleading, the actual problem is there isn't an `init` on `Int` that takes a `String`. – ABakerSmith May 20 '15 at 12:39
  • I've update my answer with a bit about your first example. If either of the answers solved your problem would you consider marking one as correct? – ABakerSmith May 20 '15 at 12:48
  • Done. Sorry - I didn't do it earlier...I'll have to make an excuse that I am still new :) – Neli Chakarova May 21 '15 at 08:48
0

What you need is this:

let height = "3"
let number = 4
let heightNumber = number + height.toInt()!

If you want to get an Int from a String you use toInt().

Vasil Garov
  • 4,851
  • 1
  • 26
  • 37
  • Yes, I know that. But even according to the error description it make sense to be able to perform the operation. It is somehow casting the height to Int. And I am wondering why is that. – Neli Chakarova May 20 '15 at 12:35
  • That's becase you want to initialize an `Int` with a `String` which is not allowed. You can only init `Int` with an integer. For example - `let a = Int(25)`. – Vasil Garov May 20 '15 at 12:39