xcode 为 println 连接混合类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24188606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate mixed types for println
提问by kmiklas
I am trying to concatenate a string and an integer, and log to the console using println
.
我正在尝试连接一个字符串和一个整数,并使用 .log 登录到控制台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.
webViewLoads 是“Int”类型。由于我在这里混合了两种类型,因此出现错误也就不足为奇了: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)
所以,我尝试将 webViewLoads 转换as
为字符串: println("Load: " + webViewLoads as String)
Grr.. Error still thrown.
Grr .. 错误仍然抛出。
How can I make this simple little concatenation work?
我怎样才能让这个简单的小串联工作?
回答by Mick MacCallum
You have a couple of options. You can create a new String from the Int and concatenate it, or you can use string interpolation.
你有几个选择。您可以从 Int 创建一个新的 String 并将其连接起来,或者您可以使用字符串插值。
println("Load number: " + String(webViewLoads))
println("Load number: \(webViewLoads)")
回答by PREMKUMAR
Check below code:
检查以下代码:
let string1 = "This is"
let intValue = 45
var appendString = "\(string1) \(intValue)"
println("APPEND STRING:\(appendString)")
回答by Greg A
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)
(frameWidth 为:var frameWidth = self.frame.width)