[英]Generic parameter 'T' could not be inferred when XCTAssertEqual() called
The topic title is a compiler error related to the xCode Assert Equal test in the testPrimesUpTo100ShouldBe25() method.主题标题是与 testPrimesUpTo100ShouldBe25() 方法中的 xCode Assert Equal 测试相关的编译器错误。
One thing I noticed is that the call to the calculate() method in PrimeCalculator, when called from testPrimePerformance() and testPrimesUpTo100ShouldBe25(), only 'colours up' green in testPrimePerformance().我注意到的一件事是,当从 testPrimePerformance() 和 testPrimesUpTo100ShouldBe25() 调用 PrimeCalculator 中的 calculate() 方法时,只有在 testPrimePerformance() 中“着色”为绿色。
Full swift source file is below:完整的 swift 源文件如下:
import XCTest
struct PrimeCalculator {
static func calculate(upTo max: Int) -> [Int] {
guard max > 1 else {
return []
}
var sieve = [Bool](repeating: true, count: max)
sieve[0] = false
sieve[1] = false
for number in 2 ..< max {
if sieve[number] == true {
for multiple in stride(from: number * number, to: sieve.count, by: number) {
sieve[multiple] = false
}
}
}
// collapse our results down to a single array of primes
let primes = sieve.enumerated().compactMap { $1 == true ? $0 : nil }
return primes
}
}
class AsynchronousTests: XCTestCase {
func testPrimePerformance() {
measure {
_ = PrimeCalculator.calculate(upTo: 1_000_000)
}
}
func testPrimesUpTo100ShouldBe25() {
// given
let maximumCount = 100
// when
let progress = PrimeCalculator.calculate(upTo: maximumCount) {
XCTAssertEqual($0.count, 25)
}
// then
let predicate = NSPredicate(format: "@.completedUnitCount == %@", argumentArray: [progress, maximumCount])
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: progress)
wait(for: [expectation], timeout: 10)
}
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}
}
Here这里
let progress = PrimeCalculator.calculate(upTo: maximumCount) {
XCTAssertEqual($0.count, 25)
}
you pass a “trailing closure” to the calculate()
method, which has two problems:您将“尾随闭包”传递给
calculate()
方法,该方法有两个问题:
$0
from the context.$0
的类型。 This causes the compiler error message. What you probably want is simply你可能想要的只是
let progress = PrimeCalculator.calculate(upTo: maximumCount)
XCTAssertEqual(progress.count, 25)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.