统一 Swift API: 使用未声明的类型 T
Unified Swift API: use of undeclared type T
我正在关注 John Sundell 的 post (https://www.swiftbysundell.com/posts/providing-a-unified-swift-error-api) 关于使用一个不错的 perform
函数来创建一个统一的错误 API
func perform(_ expression: @autoclosure () throws -> T,
orThrow error: Error) throws -> T {
do {
return try expression()
} catch {
throw error
}
}
func someFunction(url: URL) throws -> Data {
...
return try perform(Data(contentsOf: url),
orThrow: SearchError.dataLoadingFailed(url))
}
但它在 T
处给我 Use of undeclared type
错误。 Xcode 不是应该通过查看传入函数的 return 类型来找出类型 T 吗?
您需要声明函数签名中使用的任何类型参数:
func perform<T>(
_ expression: @autoclosure () throws -> T,
orThrow error: Error
) throws -> T { ...
我正在关注 John Sundell 的 post (https://www.swiftbysundell.com/posts/providing-a-unified-swift-error-api) 关于使用一个不错的 perform
函数来创建一个统一的错误 API
func perform(_ expression: @autoclosure () throws -> T,
orThrow error: Error) throws -> T {
do {
return try expression()
} catch {
throw error
}
}
func someFunction(url: URL) throws -> Data {
...
return try perform(Data(contentsOf: url),
orThrow: SearchError.dataLoadingFailed(url))
}
但它在 T
处给我 Use of undeclared type
错误。 Xcode 不是应该通过查看传入函数的 return 类型来找出类型 T 吗?
您需要声明函数签名中使用的任何类型参数:
func perform<T>(
_ expression: @autoclosure () throws -> T,
orThrow error: Error
) throws -> T { ...