【发布时间】:2016-03-23 22:46:14
【问题描述】:
在我的单元测试中,我有以下代码检查抛出的错误是否属于预期类型。它是通过两个相同的语句完成的,其中一个无法编译:
enum Error: ErrorType {
case SomeExpectedError
case SomeUnexpectedError
}
func functionThatThrows() throws {
throw Error.SomeExpectedError
}
// 1) this compiles fine
XCTAssertThrowsError(try functionThatThrows()) { (error) in
switch error {
case Error.SomeExpectedError: break
//everything is fine
case Error.SomeUnexpectedError: fallthrough
default:
XCTFail("Unexpected error thrown")
}
}
// 2) doesn't compiles at all
XCTAssertThrowsError(try functionThatThrows()) { (error) in
XCTAssertEqual(error as? Error, Error.SomeExpectedError)
}
第一个语句可以编译并且工作正常,但是第二个语句告诉我有两个错误:
Errors thrown from here are not handled 和 Cannot convert value of type '(Error) -> Void' to expected argument type 'String'。
这段代码可能有什么问题?错误信息是什么意思?
我正在使用 Xcode 7.3。
【问题讨论】:
标签: swift error-handling syntax-error closures