9

I am trying to concatenate a string and an integer, and log to the console using println.

println("Load number: " + webViewLoads)

webViewLoads is type 'Int'. As I'm mixing two types here, there is no surprise that I'm getting an error:
Could not find an overload for 'println' that accepts the supplied arguments.

So, I tried casting webViewLoads as a string: println("Load: " + webViewLoads as String)

Grr.. Error still thrown.

How can I make this simple little concatenation work?

rene
  • 41,474
  • 78
  • 114
  • 152
kmiklas
  • 13,085
  • 22
  • 67
  • 103

3 Answers3

22

You have a couple of options. You can create a new String from the Int and concatenate it, or you can use string interpolation.

println("Load number: " + String(webViewLoads))
println("Load number: \(webViewLoads)")
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
0

Check below code:

let string1 = "This is"
let intValue = 45
var appendString = "\(string1) \(intValue)"
println("APPEND STRING:\(appendString)")
PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51
  • 1
    He is asking how to append an Int to a String. You are showing two Strings. For the same reason, it is not a duplicate of the other question you mention in the comments. – Eduardo Jun 12 '14 at 16:05
0

I don't think this was mentioned, but this worked for me:

println("Frame Width: " + String(stringInterpolationSegment: frameWidth))

(frameWidth is: var frameWidth = self.frame.width)

Greg A
  • 531
  • 4
  • 9