我正忙於IBM Swift沙箱的測試版;有沒有人知道爲什麼我得到下面的代碼的以下錯誤?:IBM Swift Sandbox無法解析對CoreFoundation函數的調用
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")
}
'進口Foundation'以及...不知道爲什麼這是需要(?人),但檢查了它在一分鐘前,它的工作 – Alladinian
@Alladinian你應該回答,這是解決方案。 – Moritz
@EricD。完成。起初我猶豫不決,因爲我無法提供真正的解釋,爲什麼這是行得通的,但我想半答案總比沒有答案好。 – Alladinian