实例方法
map(_:
map(_:_:)
将两个关键路径的值发布为一个元组。
声明
func map<T0, T1>(
_ keyPath0: KeyPath<Self.Output, T0>,
_ keyPath1: KeyPath<Self.Output, T1>
) -> Publishers.MapKeyPath2<Self, T0, T1>
返回值
将两个键路径的值作为元组发布的发布者。
参数
keyPath0
Output
上的属性的关键路径。keyPath1
Output
上另一个属性的关键路径。
详述
在以下示例中,Publisher/map(_:_:)
运算符使用 Swift 键路径语法来访问由 Just
发布者发布的 DiceRoll
结构的 die1
和 die2
成员。
下游接收器订阅者仅接收这两个值(作为 (Int, Int)
元组),而不是整个 DiceRoll
。
struct DiceRoll {
let die1: Int
let die2: Int
}
cancellable = Just(DiceRoll(die1:Int.random(in:1...6),
die2: Int.random(in:1...6)))
.map(\.die1, \.die2)
.sink { values in
print ("Rolled: \(values.0), \(values.1) (total: \(values.0 + values.1))")
}
// Prints "Rolled: 6, 4 (total: 10)" (or other random values).
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
- Swift Result.Publisher mapError(_:)用法及代码示例
- Swift Result.Publisher max()用法及代码示例
- Swift Result.Publisher makeConnectable()用法及代码示例
- Swift Result.Publisher merge(with:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:)用法及代码示例
- Swift Result.Publisher min()用法及代码示例
- Swift Result.Publisher multicast(_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher multicast(subject:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:)用法及代码示例
- Swift Result.Publisher measureInterval(using:options:)用法及代码示例
- Swift Result.Publisher zip(_:_:_:)用法及代码示例
- Swift Result.Publisher tryLast(where:)用法及代码示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代码示例
- 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:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher map(_:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。