I have the following:
- Two interesting classes: a
ViewControllerand aViewModel - A button
nsButtonMorePlease:NSButtonin theviewofViewController - A text box
nsTextView:NSTextViewin theviewas well
I want the following behavior:
- When you launch the program, the "count" starts at 0 and is displayed in the text box
nsTextView - When you press the button
nsButtonMorePlease, the count is incremented by1and the updated count is reflected in thensTextView
I would like to ensure:
- I use
ReactiveCocoa 4(that's the point kind of) - The model class contains
numberOfBeans: MutableProperty<Int>starting at0 - The design is purely functional or close to it - that is (if I understand the term), every link in the chain mapping the event of mouse click to the
MutablePropertyofnumberOfBeansto responding to it in the text view, is all without side effects.
Here's what I have. Fair warning: this doesn't come close to working or compiling I believe. But I do feel like maybe I want to use one of combineLatest, collect, reduce, etc. Just lost on what to do specifically. I do feel like this makes something easy quite hard.
class CandyViewModel {
private let racPropertyBeansCount: MutableProperty<Int> = MutableProperty<Int>(0)
lazy var racActionIncrementBeansCount: Action<AnyObject?, Int, NoError> = {
return Action { _ in SignalProducer<Int, NoError>(value: 1)
}
}()
var racCocoaIncrementBeansAction: CocoaAction
init() {
racCocoaIncrementBeansAction = CocoaAction.init(racActionIncrementBeansCount, input: "")
// ???
var sig = racPropertyBeansCount.producer.combineLatestWith(racActionIncrementBeansCount.)
}
}
class CandyView: NSViewController {
@IBOutlet private var guiButtonMoreCandy: NSButton!
@IBOutlet private var guiTextViewCandyCt: NSTextView!
}