Created
November 23, 2021 08:36
-
-
Save brownsoo/a45f4dc36dda936cb2ebbed9ec8fbde8 to your computer and use it in GitHub Desktop.
KVO Binding sample
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
/// 바인딩 | |
private var observations: [NSKeyValueObservation] = [] | |
private var viewModel = ExpenseViewModel() | |
// 뷰 바인딩 | |
observations = [ | |
viewModel.observe(\.price) { | |
let price = $0 ?? 0.0 | |
self.updatePriceText(with: price) | |
self.changesPublisher.onNext(1) | |
}, | |
viewModel.observe(\.title) { [tfTitle] in | |
if tfTitle.isFirstResponder == false { | |
tfTitle.text = $0 | |
} | |
self.limitTitle($0) | |
self.changesPublisher.onNext(1) | |
}, | |
viewModel.observe(\.text) { [tvContents, placeHolder] in | |
let text = $0 as? String | |
if tvContents.isFirstResponder == false { | |
tvContents.text = text | |
placeHolder.isHidden = (text?.count ?? 0) > 0 | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment