實例方法
set
setFailureType(to:)
更改上遊發布者聲明的失敗類型。
聲明
func setFailureType<E>(to failureType: E.Type) -> Publishers.SetFailureType<Self, E> where E : Error
當
Failure
為 Never
時可用。返回值
似乎發送指定失敗類型的發布者。
參數
failureType
此發布者提供的
Failure
類型。
詳述
當您需要設置不能失敗的發布者的錯誤類型時,使用Publisher/setFailureType(to:)
。
相反,如果上遊可能失敗,您將使用Publisher/mapError(_:)
提供有關將錯誤類型轉換為下遊發布者輸入所需的說明。
以下示例有兩個錯誤類型不匹配的發布者: pub1
的錯誤類型是
, Never
pub2
的錯誤類型是
。由於不匹配,Error
Publisher/combineLatest(_:)
運算符要求pub1
使用Publisher/setFailureType(to:)
以使pub1
可以產生
類型,就像Error
pub2
一樣。
let pub1 = [0, 1, 2, 3, 4, 5].publisher
let pub2 = CurrentValueSubject<Int, Error>(0)
let cancellable = pub1
.setFailureType(to: Error.self)
.combineLatest(pub2)
.sink(
receiveCompletion: { print ("completed: \($0)") },
receiveValue: { print ("value: \($0)")}
)
// Prints: "value: (5, 0)".
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Optional.Publisher sink(receiveValue:)用法及代碼示例
- Swift Optional.Publisher scan(_:_:)用法及代碼示例
- Swift Optional.Publisher share()用法及代碼示例
- Swift Optional.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Optional.Publisher subscribe(on:options:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift Optional.Publisher debounce(for:scheduler:options:)用法及代碼示例
- Swift Optional.Publisher breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)用法及代碼示例
- Swift Optional.Publisher mapError(_:)用法及代碼示例
- Swift Optional.Publisher catch(_:)用法及代碼示例
- Swift Optional.Publisher tryAllSatisfy(_:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代碼示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代碼示例
- Swift Optional.Publisher collect(_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher merge(with:_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:)用法及代碼示例
- Swift Optional.Publisher assign(to:on:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher output(in:)用法及代碼示例
- Swift Optional.Publisher tryReduce(_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:_:)用法及代碼示例
- Swift Optional.Publisher prepend(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional.Publisher setFailureType(to:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。