This file contains hidden or 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
| struct Book { | |
| var name: String | |
| } | |
| class BookStore: ObservableObject { | |
| @Published var books: [Book] = [Book(name: "The Great Influenza")] | |
| public func addBook(name: String) { | |
| self.books.append(Book(name: name)) | |
| } |
This file contains hidden or 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
| struct ContentView: View { | |
| @State var dataStore = ["A", "B", "C"] | |
| var body: some View { | |
| HStack { | |
| ChildViewA(characters: dataStore) | |
| Spacer() | |
| ChildViewB(characters: dataStore) |
This file contains hidden or 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
| struct ContentView: View { | |
| @State private var cannotProceed = false | |
| var body: some View { | |
| VStack { | |
| Button("Toggle cannotProceed") { | |
| cannotProceed.toggle() | |
| } | |
| Button("Proceed") { |
This file contains hidden or 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
| import Combine | |
| class LoginViewModel { | |
| public var username = CurrentValueSubject<String, Never>("") | |
| public var password = CurrentValueSubject<String, Never>("") | |
| public var confirmPassword = CurrentValueSubject<String, Never>("") | |
| public var isValid = false { | |
| didSet { | |
| print("Inputs are valid: \(isValid)") |
This file contains hidden or 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
| import Combine | |
| publisher1.combineLatest(publisher2) |
This file contains hidden or 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
| import Combine | |
| let integerConvertSubject = PassthroughSubject<String, Never>() | |
| let integerConvertSubscription = integerConvertSubject | |
| .compactMap({ value in | |
| // Int initializer returns an optional | |
| return Int(value) | |
| }) | |
| .sink { value in | |
| print("value came out of compact map: \(value)") |
This file contains hidden or 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
| import Combine | |
| let subscription1 = [0, 1, 1].publisher | |
| .removeDuplicates() | |
| .sink { value in | |
| print(value) | |
| } | |
| // 0 | |
| // 1 |
This file contains hidden or 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
| import Combine | |
| let intList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
| intList.publisher | |
| .filter({ $0 % 2 == 0 }) | |
| .map({ String("Received integer: \($0)") }) | |
| .sink { result in | |
| print(result) | |
| } |
This file contains hidden or 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
| import Combine | |
| class MapDemo { | |
| var text: String = "" { | |
| didSet { | |
| print(text) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| import Combine | |
| let currentvalue = CurrentValueSubject<Int, Never>(0) | |
| print(currentvalue.value) // prints: 0 | |
| currentvalue.value = 10 | |
| print(currentvalue.value) // prints: 10 | |
| let currentvalueSubscription = currentvalue.sink { value in | |
| print(value) | |
| } |