本文僅代表個(gè)人看法耕捞,有意見或者不服矾踱,你可以順著網(wǎng)線來(lái)打我!(開玩笑的卢肃, 盡情指點(diǎn)我這個(gè)小彩筆)
看了個(gè)OC版的MVVM的簡(jiǎn)單Demo, 手癢就寫了個(gè)Swift的
MVVM這個(gè)框架的知識(shí)我就不說(shuō)了才顿, 網(wǎng)上一大堆莫湘,但是看起好麻煩。所以我就寫了一個(gè)二者區(qū)別以便更好的理解VF(之前看別人寫關(guān)于MVVM的幅垮, 看起來(lái)好復(fù)雜, 好難尾组, 研究了之后才知道其實(shí)并沒有那么難)
4.png
怎么說(shuō)呢忙芒,MVVM其實(shí)是把ViewController里面的邏輯處理放在ViewModel里面進(jìn)行處理了
viewModel里面的代碼:
import UIKit
import Alamofire
import SwiftyJSON
class MovieViewModel: NSObject {
// 獲取數(shù)據(jù), 這個(gè)本來(lái)是放在HomeViewController里面的演怎, 現(xiàn)在放在ViewModel了
func getData(complete:@escaping (_ array: [MovieModel]) -> Void) {
let url = HEAD_URL + "/v2/movie/coming_soon"
Alamofire.request(url, method: .post).responseJSON { (response) in
if let data = response.result.value {
let json = JSON(data)
var array = [MovieModel]()
let subjects = json["subjects"].arrayValue
for subject in subjects {
let model = MovieModel()
model.movieName = subject["title"].stringValue
model.year = subject["year"].stringValue
model.imageUrl = subject["images"]["medium"].stringValue
model.detailUrl = subject["alt"].stringValue
array.append(model)
}
complete(array)
}
else {
print(response.error)
}
}
}
// 這里其實(shí)是HomeViewController里面 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 里面執(zhí)行的代碼, 也是換了位置
func movieDetailWithPublicModel(movieModel: MovieModel, superController: UIViewController) {
let movieVC = MovieViewController()
movieVC.url = movieModel.detailUrl
superController.navigationController?.pushViewController(movieVC, animated: true)
}
}
再看看HomeViewController里面
import UIKit
class HomeViewController: UIViewController {
var array = [MovieModel]()
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setUI()
}
func setUI() {
self.title = "電影首頁(yè)"
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: width, height: height), style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 80
self.view.addSubview(tableView)
tableView.register(UINib(nibName: "MovieCell", bundle: nil), forCellReuseIdentifier: "Cell")
// 調(diào)用ViewModel 的 getData 閉包 獲取數(shù)據(jù)避乏! 對(duì)應(yīng)ViewModel的func getData(complete:@escaping (_ array: [MovieModel]) -> Void) 方法
let model = MovieViewModel()
model.getData { [weak self] (dataArray) in
self!.array = dataArray
self!.tableView.reloadData()
}
}
}
extension HomeViewController: UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MovieCell
cell.model = array[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let movieModel = MovieViewModel()
movieModel.movieDetailWithPublicModel(movieModel: array[indexPath.row], superController: self)
}
}
看到這樣有人會(huì)問了爷耀,既然只是代碼換了位置,那MVVM有什么用拍皮?
耦合更低歹叮, 代碼維護(hù)更方便,邏輯代理處理更加容易铆帽! 而且還有MVVM+RAC我還沒研究咆耿,研究了我再告訴你有什么用!
有興趣的可以下Demo看看:https://github.com/BJGX/MVVM-Demo