CryptoTicker-MVVM

所属分类:Swift编程
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2024-01-24 15:24:57
上 传 者sh-1993
说明:  具有协议、闭包、 React编程(RxSwift)和组合场景的简单MVVM示例项目。
(Simple MVVM example project with protocol, closure, reactive programming(RxSwift), and combine scenarios.)

文件列表:
CryptoTicker.xcodeproj/
CryptoTicker.xcworkspace/
CryptoTicker/
CryptoTickerTests/
CryptoTickerUITests/
Pods/
Podfile
Podfile.lock

# CryptoTicker Simple MVVM example project with protocol, closure, reactive programming(RxSwift), and comebine scenarios. If you would like to understand the idea behind it, you can read the article about the project in [Medium](https://demirciy.medium.com/mvvm-in-ios-development-with-protocol-closure-reactive-programming-rxswift-d0933b235235). The project has 2 simple screens which are list and coin. In the list, can be seen the coins in a list and searched for them. In the coin, the coin model is passed from the list to here and is showed its symbol in the navigation bar title. List Search Coin Please change the branches to see the scenarios. ## Protocol The controller and view model communicates with protocol functions **View Model** ```swift protocol ListDelegate { func coinsDidRefresh() } func refreshCoins() { delegate?.coinsDidRefresh() } ``` **Controller** ```swift func coinsDidRefresh() { tableView.reloadData() } ``` ## Closure The controller and view model communicates with closures **View Model** ```swift var coinsDidRefresh: (() -> Void)? func refreshCoins() { coinsDidRefresh?() } ``` **Controller** ```swift viewModel.coinsDidRefresh = { _ in tableView.reloadData() } ``` ## Reactive The controller and view model communicates with reactive programming **View Model** ```swift let coins: BehaviorRelay<[Coin]> = .init(value: []) func refreshCoins() { coins.accept(getDummyCoins()) } ``` **Controller** ```swift viewModel.coins.bind(to: tableView.rx.items(cellIdentifier: "ListCell", cellType: UITableViewCell.self)) { index, model, cell in cell.textLabel?.text = model.symbol cell.detailTextLabel?.text = "\(model.price)" } .disposed(by: disposeBag) ``` ## Combine The controller and view model communicates with Combine framework **View Model** ```swift @Published var coins: [Coin] = [] func refreshCoins() { coins = getDummyCoins() } ``` **Controller** ```swift viewModel.$coins .sink { coins in tableView.reloadData() } .store(in: &subscribers) ```

近期下载者

相关文件


收藏者