实例方法
replace
replaceNil(with:)
用提供的元素替换流中的 nil 元素。
声明
func replaceNil<T>(with output: T) -> Publishers.Map<Self, T> where Self.Output == T?
返回值
使用提供的元素替换上游发布者的nil
元素的发布者。
参数
output
替换
nil
时要使用的元素。
详述
Publisher/replaceNil(with:)
运算符允许用替换值替换流中的nil
值。在下面的示例中,集合发布者包含一个 nil 值。 Publisher/replaceNil(with:)
运算符将其替换为 0.0
。
let numbers: [Double?] = [1.0, 2.0, nil, 3.0]
numbers.publisher
.replaceNil(with: 0.0)
.sink { print("\($0)", terminator: " ") }
// Prints: "Optional(1.0) Optional(2.0) Optional(0.0) Optional(3.0)"
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher receive(on:options:)用法及代码示例
- Swift Result.Publisher reduce(_:_:)用法及代码示例
- Swift Result.Publisher removeDuplicates()用法及代码示例
- Swift Result.Publisher zip(_:_:_:)用法及代码示例
- Swift Result.Publisher tryLast(where:)用法及代码示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:)用法及代码示例
- Swift Result.Publisher tryCompactMap(_:)用法及代码示例
- 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 tryCatch(_:)用法及代码示例
- Swift Result.Publisher dropFirst(_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代码示例
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher catch(_:)用法及代码示例
- Swift Result.Publisher drop(while:)用法及代码示例
- Swift Result.Publisher prefix(_:)用法及代码示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代码示例
- Swift Result.Publisher merge(with:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher replaceNil(with:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。