swift3.0 MVVM與MVC區(qū)別

本文僅代表個人看法,有意見或者不服,你可以順著網(wǎng)線來打我?。ㄩ_玩笑的, 盡情指點我這個小彩筆)
看了個OC版的MVVM的簡單Demo, 手癢就寫了個Swift的

MVVM這個框架的知識我就不說了, 網(wǎng)上一大堆,但是看起好麻煩。所以我就寫了一個二者區(qū)別以便更好的理解?。ㄖ翱磩e人寫關(guān)于MVVM的, 看起來好復(fù)雜, 好難, 研究了之后才知道其實并沒有那么難)

4.png

怎么說呢,MVVM其實是把ViewController里面的邏輯處理放在ViewModel里面進(jìn)行處理了
viewModel里面的代碼:

import UIKit
import Alamofire
import SwiftyJSON

class MovieViewModel: NSObject {
  // 獲取數(shù)據(jù), 這個本來是放在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)
      }

    }
  }
  
// 這里其實是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 = "電影首頁"
    
    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ù)! 對應(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)
  }
}

看到這樣有人會問了,既然只是代碼換了位置,那MVVM有什么用?
耦合更低, 代碼維護(hù)更方便,邏輯代理處理更加容易! 而且還有MVVM+RAC我還沒研究,研究了我再告訴你有什么用!
有興趣的可以下Demo看看:https://github.com/BJGX/MVVM-Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容