【问题标题】:IBM Swift Sandbox cannot resolve call to CoreFoundation functionIBM Swift Sandbox 无法解析对 CoreFoundation 函数的调用
【发布时间】:2015-12-05 12:57:58
【问题描述】:

我在玩 IBM Swift Sandbox 的 beta 版;有谁知道为什么我在下面的代码中收到以下错误?:

LLVM 错误:程序使用了无法解析的外部函数 'CFAbsoluteTimeGetCurrent'!

// A demonstration of both iterative and recursive algorithms for computing the Fibonacci numbers.

import CoreFoundation

// A recursive algorithm to compute the Fibonacci numbers.
func fibRec (n : Int) -> Double {
    return (Double)(n < 3 ? 1 : fibRec(n - 1) + fibRec(n - 2))
}

// An iterative algorithm to compute the Fibonacci numbers.
func fibIter (n : Int) -> Double {
    var f2 = 0.0
    var f1 = 1.0
    var f0 = 1.0

    for _ in 0 ..< n {
        f2 = f1 + f0
        f0 = f1
        f1 = f2
    }
    return f0
}

// Initialise array to hold algorithm execution times.
var fibTimes = [Double]()

// i is the ith Fibonacci number to be computed.
for i in 120..<129 {

    var fibNum = 0.0
    var fibSum = 0.0

    // j is the number of times to compute F(i) to obtain average.
    for j in 0..<5 {

        // Set start time.
        let startTime = CFAbsoluteTimeGetCurrent()

        // Uses the recursive algorithm.
        //                fibNum = fibRec(i)

        // Uses the iterative algorithm.
        fibNum = fibIter(i)
        fibTimes.insert(CFAbsoluteTimeGetCurrent() - startTime, atIndex: j)
    }

    // Compute the average execution time.
    for p in fibTimes {
        fibSum += p
    }
    fibSum = fibSum / 5

    print("Fibonacci number \(i) is: \(fibNum)")
    print("Execution time:         \(fibSum) seconds")
}

【问题讨论】:

  • import Foundation 也...不知道为什么需要这样做(任何人?),但在一分钟前检查过它并且它正在工作
  • @Alladinian 你应该回答,这就是解决方案。
  • @EricD。完毕。起初我很犹豫,因为我无法提供一个真正解释为什么这是有效的,但我想半答案总比没有答案好。

标签: swift ibm-swift-sandbox


【解决方案1】:

您也只需import Foundation

我不确定为什么需要这样做(我希望知道的人能对此有所了解),但它确实有效。

【讨论】:

    【解决方案2】:

    为了澄清,您必须同时导入它们。

    import Foundation
    import CoreFoundation
    

    另外,Glibc 有 clock() ,它返回一个 Int 。例如:

    import Glibc
    struct StartTimer {
        let start = clock()
        var elapsed = -1
        mutating func stop() { elapsed = clock() - start }
    }
    
    var timer = StartTimer()
    /* do your work */
    timer.stop()
    print("Elapsed Time: \(timer.elapsed)")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2017-10-12
      相关资源
      最近更新 更多