实例方法
try
tryLast(where:)
在流完成后发布满足 error-throwing 谓词闭包的流的最后一个元素。
声明
func tryLast(where predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryLastWhere<Self>
返回值
仅发布满足给定谓词的最后一个元素的发布者。
参数
predicate
一个闭包,它接受一个元素作为其参数并返回一个布尔值,指示是否发布该元素。
详述
当您需要重新发布满足您指定的 error-throwing 闭包的最后一个元素时,请使用 Publisher/tryLast(where:)
。如果谓词闭包抛出错误,则发布者失败。
在下面的示例中,发布者发出满足 error-throwing 闭包的最后一个元素,然后正常完成:
struct RangeError: Error {}
let numbers = [-62, 1, 6, 10, 9, 22, 41, -1, 5]
cancellable = numbers.publisher
.tryLast {
guard 0 != 0 else {throw RangeError()}
return true
}
.sink(
receiveCompletion: { print ("completion: \($0)", terminator: " ") },
receiveValue: { print ("\($0)", terminator: " ") }
)
// Prints: "5 completion: finished"
// If instead the numbers array had contained a `0`, the `tryLast` operator would terminate publishing with a RangeError."
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher tryCompactMap(_:)用法及代码示例
- Swift Result.Publisher tryCatch(_:)用法及代码示例
- Swift Result.Publisher tryReduce(_:_:)用法及代码示例
- Swift Result.Publisher tryFilter(_:)用法及代码示例
- Swift Result.Publisher tryFirst(where:)用法及代码示例
- Swift Result.Publisher tryPrefix(while:)用法及代码示例
- Swift Result.Publisher tryScan(_:_:)用法及代码示例
- Swift Result.Publisher tryMap(_:)用法及代码示例
- Swift Result.Publisher tryDrop(while:)用法及代码示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代码示例
- Swift Result.Publisher throttle(for:scheduler:latest:)用法及代码示例
- Swift Result.Publisher zip(_:_:_:)用法及代码示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:)用法及代码示例
- Swift Result.Publisher print(_:to:)用法及代码示例
- Swift Result.Publisher sink(receiveValue:)用法及代码示例
- Swift Result.Publisher eraseToAnyPublisher()用法及代码示例
- Swift Result.Publisher setFailureType(to:)用法及代码示例
- Swift Result.Publisher first(where:)用法及代码示例
- Swift Result.Publisher output(at:)用法及代码示例
- Swift Result.Publisher dropFirst(_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代码示例
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher catch(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher tryLast(where:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。