我正在尝试计算我的 swift 程序的执行时间。我正在尝试使用 CFAbsoluteTimeGetCurrent()
,但收到错误:sum.swift:29:19: error: 在范围内找不到 'CFAbsoluteTimeGetCurrent'
我看到在 Mac 上可以通过重新启动 Xcode 来解决此问题,但我使用的是 Linux,找不到任何解决方案。
这是我的完整代码
import Foundation
// User prompt
print("Enter a digit between 0 and 9:", terminator: " ")
// readLine() returns optional string
let input1: String? = readLine()
// User prompt
print("Enter another digit between 0 and 9:", terminator: " ")
// readLine() returns optional string
let input2: String? = readLine()
// Recording execution Time
let startTime = CFAbsoluteTimeGetCurrent()
// Converting String input to Int
if let n1 = Int(input1!), // Checking if optional String can be converted to Int
let n2 = Int(input2!),// Checking if optional String can be converted to Int
n1 >= 0 && n1 <= 9, // Checking if it's single digit
n2 >= 0 && n2 <= 9 { // Checking if it's single digit
print("Sum: \(n1+n2)")
} else {
preconditionFailure("Input conditions not met!!")
}
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time elapsed: \(timeElapsed) s.")
最佳答案
我通过导入 CoreFoundation
解决了这个问题。
关于swift - sum.swift :29:19: error: cannot find 'CFAbsoluteTimeGetCurrent' in scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68148071/