Last active
February 27, 2020 18:27
-
-
Save below/2fa8f9f4d7840ae37be897065d9bcc36 to your computer and use it in GitHub Desktop.
A simple combine sample. Good code? Could it be better?
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
class InOut: ObservableObject { | |
@Published var input = "" | |
@Published private (set) var output = "output" | |
private var publisher: AnyCancellable? | |
init() { | |
publisher = $input.map({ | |
String($0.reversed()) | |
}).assign(to: \.output, on: self) | |
} | |
} |
Updated with some helpful input
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could avoid the force unwrap of publisher if you change it to an array and initialize it before as an empty array. Then append the cancellable that you created in init to this array. This way you could also manage several cancellables.