My app has a PhraseModel object that includes multiple WordModel objects (each word publishes a currentWord value)
I now want to add a pipeline that will eventually include debouncing, and possibly even network calls. But for now, I just want to verify that I can declare a basic CombineLatest operator and create a subscriber that fires anytime one of the words changes.
(should this exist in the model object? or in the View?)
Currently, I don't seem to be able to get my basic baby steps code to compile. (problems in the code below around line 10 and line 24.)
I'd be grateful for any high level or low level help on this. I feel like I've read fairly extensively, but nothing I've come across seems to apply perfectly to what I'm doing. (in particular the example at the 26 minute mark of video 721 from wwdc2019 feels tantalizing close. alas it doesn't seem to compile either.)
thanks, in advance,
Mike
I now want to add a pipeline that will eventually include debouncing, and possibly even network calls. But for now, I just want to verify that I can declare a basic CombineLatest operator and create a subscriber that fires anytime one of the words changes.
(should this exist in the model object? or in the View?)
Currently, I don't seem to be able to get my basic baby steps code to compile. (problems in the code below around line 10 and line 24.)
I'd be grateful for any high level or low level help on this. I feel like I've read fairly extensively, but nothing I've come across seems to apply perfectly to what I'm doing. (in particular the example at the 26 minute mark of video 721 from wwdc2019 feels tantalizing close. alas it doesn't seem to compile either.)
thanks, in advance,
Mike
Code Block class WordModel: ObservableObject { @Published var currentWord: String init(word: String) { self.currentWord = word } } class PhraseModel: ObservableObject { let leftWord = WordModel(word: "myLeft") let rightWord = WordModel(word: "myRight") var phraseChangedPublisher: AnyPublisher<String, Never> { return CombineLatest(leftWord.$currentWord, rightWord.$currentWord) .map{(leftWord, rightWord) -> String return leftWord + rightWord } } } struct PhraseView: View { @ObservedObject var model: PhraseModel var body: some View { HStack { Text(model.leftWord.currentWord) Text(model.rightWord.currentWord) } .onReceive(model.phraseChangedPublisher, perform: { print("hello") }) } }