gpt4 book ai didi

swift - XCTAssertNoThrow 在闭包中调用时不编译

转载 作者:可可西里 更新时间:2023-11-01 00:55:55 26 4
gpt4 key购买 nike

尝试编译一个在闭包中使用 XCTAssertNoThrow 的测试用例,但没有成功。怎么这么死板,是rethrow还是怎么回事?

let e = expectation(description: "Wait")

distillery.produce(request: Request(bottles: 1337)) { (result) in // error
XCTAssertNoThrow(try result.unwrap())

e.fulfill()
}

Xcode 在编译期间抛出错误:

Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(Result) -> Void'

ps:我认为这似乎是 Swift 中的一个错误 https://bugs.swift.org/browse/SR-487

最佳答案

通过将 XCTAssertNoThrow 包装到辅助函数中解决了这个问题

func assertOk<T>(_ result: Result<T>, file: StaticString = #file, line: UInt = #line) {
XCTAssertNoThrow(try result.unwrap(), file: file, line: line)
}

关于swift - XCTAssertNoThrow 在闭包中调用时不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com