實例方法
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(_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。