當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Swift Result.Publisher breakpointOnError()用法及代碼示例


實例方法

breakpointOnError()

在收到故障時引發調試器信號。

聲明

func breakpointOnError() -> Publishers.Breakpoint<Self>

返回值

發布者在收到失敗時引發調試器信號。

詳述

當上遊發布者因錯誤而失敗時,該發布者會引發 SIGTRAP 信號,從而停止調試器中的進程。否則,此發布者按原樣傳遞值和完成。

在此示例中,PassthroughSubject 發布字符串,但其下遊 Publisher/tryMap(_:) 運算符引發錯誤。這會將錯誤作為 Subscribers/Completion/failure(_:) 發送到下遊。 Publisher/breakpointOnError() 運算符接收此完成並在調試器中停止應用程序。


 struct CustomError : Error {}
 let publisher = PassthroughSubject<String?, Error>()
 cancellable = publisher
     .tryMap { stringValue in
         throw CustomError()
     }
     .breakpointOnError()
     .sink(
         receiveCompletion: { completion in print("Completion: \(String(describing: completion))") },
         receiveValue: { aValue in print("Result: \(String(describing: aValue))") }
     )


 publisher.send("TEST DATA")


 // Prints: "error: Execution was interrupted, reason: signal SIGTRAP."
 // Depending on your specific environment, the console messages may
 // also include stack trace information, which is not shown here.

可用版本

iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+

相關用法


注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher breakpointOnError()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。