Created
March 3, 2022 19:52
-
-
Save EricRabil/ea1c6a49121455515ce5c27eb0669474 to your computer and use it in GitHub Desktop.
Example of Swift Combine CurrentValueSubject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// | |
// Created by Eric Rabil on 3/3/22. | |
// | |
import Foundation | |
import Combine | |
let foo = CurrentValueSubject<String, Never>("bar") | |
var bag = Set<AnyCancellable>() | |
foo.sink { newValue in | |
print(newValue) | |
}.store(in: &bag) | |
foo.sink { newValue in | |
print(newValue) | |
}.store(in: &bag) | |
foo.send("baz") | |
/* | |
Output: | |
bar | |
bar | |
baz | |
baz | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment