Skip to content

Instantly share code, notes, and snippets.

@below below/InOut.swift

Last active Feb 27, 2020
Embed
What would you like to do?
A simple combine sample. Good code? Could it be better?
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)
}
}
@mmadeveloper

This comment has been minimized.

Copy link

mmadeveloper commented Feb 27, 2020

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.

@below

This comment has been minimized.

Copy link
Owner Author

below commented Feb 27, 2020

Updated with some helpful input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.