在Swift中将数字与字符串连接起来 [英] Concatenate number with string in Swift

查看:127
本文介绍了在Swift中将数字与字符串连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接一个 StringInt 如下:

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:

二元运算符+"不能应用于String"和Int"类型的操作数

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

连接 String + Int 的正确方法是什么?

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

推荐答案

如果你想把一个数字放在一个字符串中,你可以使用 字符串插值:

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

return "first \(myVariable)"

这篇关于在Swift中将数字与字符串连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆