同时是发送者和接受者
示例:使用 PassthroughSubject
PassthroughSubjectimport Combine
class MyPublisherSubscriber {
var cancellables = Set<AnyCancellable>()
var subject = PassthroughSubject<String, Never>()
init() {
// 作为 Subscriber 订阅自己发布的事件
subject
.sink { value in
print("Received: \(value)")
}
.store(in: &cancellables)
// 作为 Publisher 发布事件
subject.send("Hello, Combine!")
}
}
let example = MyPublisherSubscriber()Last updated