swift - sum.swift :29:19: error: cannot find 'CFAbsoluteTimeGetCurrent' in scope

标签 swift

我正在尝试计算我的 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/

相关文章:

ios - 如何在 Swift 中使用拉取刷新?

ios - 应用程序无法使用 3g 连接,但可以在 wifi 上正常工作

ios - UIGraphicsGetImageFromCurrentImageContext() 返回 nil

swift: "(AnyObject)"是什么类型?

swift - 我的按钮在 iPhone 6 和 6 plus 上居中,但在 iPhone 5 上不居中

swift - UICollectionViewCell 注册类失败,但注册 nib 有效

ios - 如何在Google Map,Swift中设置受限区域或受限区域

swift - Swift 3 中非转义闭包的问题

ios - 从 Facebook 获取照片

ios - 如何在 Swift 中旋转 UIButton?