Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

nsstring - Concatenate number with string in Swift

I need to concatenate a String and Int as below:

let myVariable: Int = 8
return "first " + myVariable

But it does not compile, with the error:

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

What is the proper way to concatenate a String + Int?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to put a number inside a string, you can just use String Interpolation:

return "first (myVariable)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...