我刚进入Combine
岁,但由于某种原因,我无法进入passthrough subjects
岁.即使我有来自多个不同来源的复制粘贴示例,它们也不会打印任何内容.我试过Publishers
和CurrentValueSubjects
,效果很好,但用了PassThroughSubjects
;不.下面是我try 过的一个例子:
let mySubject = PassthroughSubject<String, Error>()
mySubject.sink(receiveCompletion: { completion in
print("-- completion", completion)
}, receiveValue: { value in
print("-- value", value)
}).cancel()
mySubject.send("one")
mySubject.send("two")
mySubject.send("three")
这是在viewDidLoad
中运行的.
正如我所说,我已经成功地try 了Publishers
和CurrentValueSubjects
:
["one", "two", "three"].publisher
.sink(receiveValue: { v in
print("-- hello", v)
}).cancel()
let subject = CurrentValueSubject<String, Error>("Initial Value")
subject.send("Hello")
subject.sink(receiveCompletion: { c in
print("-- completion", c)
}, receiveValue: { v in
print("-- value", v)
}).cancel()