如何在 Swift 中打印 NSTimeInterval?
How to println NSTimeInterval in Swift?
我想使用 println 或 NSLog 语句打印 elapsedTime
。如何将 NSTimeInterval elapsedTime
转换为 String 来打印它?
t_start = NSDate()
var elapsedTime = NSDate.timeIntervalSinceDate(t_start)
println("in applicationDidEnterBackground, elapsedTime = " + elapsedTime)
您可以使用elapsedTime.description或字符串插值
"\(elapsedTime)"
存在语法错误:
NSDate.timeIntervalSinceDate(t_start)
应该是 NSDate().timeIntervalSinceDate(t_start)
.
然后在println语句中使用String Interpolation
这是更正后的版本:
let t_start = NSDate()
let elapsedTime = NSDate().timeIntervalSinceDate(t_start)
println("in applicationDidEnterBackground, elapsedTime =\(elapsedTime)")
输出:
"in applicationDidEnterBackground, elapsedTime =0.00790297985076904"
小数点较少使用字符串格式:
println(String(format: "in applicationDidEnterBackground, elapsedTime =: %.3f sec", elapsedTime))
输出
in applicationDidEnterBackground, elapsedTime =: 0.008 sec
我想使用 println 或 NSLog 语句打印 elapsedTime
。如何将 NSTimeInterval elapsedTime
转换为 String 来打印它?
t_start = NSDate()
var elapsedTime = NSDate.timeIntervalSinceDate(t_start)
println("in applicationDidEnterBackground, elapsedTime = " + elapsedTime)
您可以使用elapsedTime.description或字符串插值
"\(elapsedTime)"
存在语法错误:
NSDate.timeIntervalSinceDate(t_start)
应该是 NSDate().timeIntervalSinceDate(t_start)
.
然后在println语句中使用String Interpolation
这是更正后的版本:
let t_start = NSDate()
let elapsedTime = NSDate().timeIntervalSinceDate(t_start)
println("in applicationDidEnterBackground, elapsedTime =\(elapsedTime)")
输出:
"in applicationDidEnterBackground, elapsedTime =0.00790297985076904"
小数点较少使用字符串格式:
println(String(format: "in applicationDidEnterBackground, elapsedTime =: %.3f sec", elapsedTime))
输出
in applicationDidEnterBackground, elapsedTime =: 0.008 sec