Concatenate mixed types for println
Asked Answered
L

3

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?

Liter answered 12/6, 2014 at 15:45 Comment(5)
use String(webViewLoads)Mention
possible duplicate of Append String in SwiftSchleicher
Also println("Load: " + (webViewLoads as String)) should workMention
@Eduardo, it should not, and won't.Digraph
@holex: you are rightMention
T
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)")
Tumefacient answered 12/6, 2014 at 15:48 Comment(0)
S
0

Check below code:

let string1 = "This is"
let intValue = 45
var appendString = "\(string1) \(intValue)"
println("APPEND STRING:\(appendString)")
Schleicher answered 12/6, 2014 at 15:50 Comment(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.Mention
P
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)

Princely answered 28/6, 2015 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.