调试print/handleEvents
1. print
printimport Combine
let publisher = Just("Hello, Combine!")
.print() // 将所有事件输出到控制台
let cancellable = publisher.sink(receiveCompletion: { completion in
print("完成: \(completion)")
}, receiveValue: { value in
print("值: \(value)")
})2. handleEvents
handleEventsimport Combine
let publisher = Just("Hello, Combine!")
.handleEvents(receiveOutput: { value in
print("接收到值: \(value)")
}, receiveCompletion: { completion in
print("完成: \(completion)")
}, receiveCancel: {
print("取消操作")
}, receiveRequest: { request in
print("请求: \(request)")
})
let cancellable = publisher.sink(receiveCompletion: { completion in
print("最终完成: \(completion)")
}, receiveValue: { value in
print("最终值: \(value)")
})总结
Last updated