1.ViewModel层,就是View和Model层的粘合剂
2.View层就是ViewController
3.Model层就是用于处理数据的层
项目结构
一,model
//
// WKModel.swift
// swift-mvvm
//
// Created by Admin on 2019/2/11.
// Copyright © 2019 Admin. All rights reserved.
//
import . Foundation
import SwiftyJSON
protocol WKModelDelegete {
func getDataError(error:String)
func getDataSucess(items:Array)
}
class WKModel {
var delegate :WKModelDelegete!
init(delegate:WKModelDelegete) {
self.delegate=delegate
}
func getServiceData(){
let dic = ["key":"c73b082b0c150b3bcba2cea1b96a8922"]
AFNetWorkingUtil.shared.request(requestType: .GET, urlString:"/dream/category", parameters: dicas[String:AnyObject], requestSuccess: { (response)in
var itmes:Array= [String]()
let json =JSON(response!)
let d = json["result"].array!.count
if d>0{
let list:Array = json["result"].array!
for item in list {
itmes.append(item["name"].string!)
}
self.delegate.getDataSucess(items: itmes)
}
}) { (error)in
self.delegate.getDataError(error:"服务器异常")
}
}
}
二,controller
//
// WKMVVMDemo.swift
// swift-mvvm
//
// Created by Admin on 2019/2/11.
// Copyright © 2019 Admin. All rights reserved.
//
importToast_Swift
import UIKit
class WKMVVMDemo: UITableViewController,WKView {
var items:Array= [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.title="Get请求"
let vm =WKViewModel(view:self)
vm.getServiceData()
}
func showProgress() {
MBProgressHUD.showAdded(to:self.view, animated:true)
}
funchideProgress() {
MBProgressHUD.hide(for:self.view, animated:true)
}
func getWKData(items:Array) {
self.items= items
self.tableView.reloadData()
}
override func tableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return items.count
}
override func tableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
let cell =UITableViewCell()
cell.textLabel?.text=items[indexPath.row]
returncell
}
override func tableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
self.view.makeToast(items[indexPath.row])
}
}
三,viewModel
//
// WKViewModel.swift
// swift-mvvm
//
// Created by Admin on 2019/2/11.
// Copyright © 2019 Admin. All rights reserved.
//
importFoundation
class WKViewModel:WKModelDelegete{
// model
varmodel:WKModel!
//view
var view :WKView
init(view:WKView) {
self.view= view
self.model=WKModel(delegate:self)
}
func getServiceData() {
self.view.showProgress()
self.model.getServiceData()
}
func getDataError(error:String) {
self.view.hideProgress()
}
func getDataSucess(items:Array) {
self.view.hideProgress()
self.view.getWKData(items: items)
}
}
四,afnetwork的封装
//
// AFNetWorkingUtil.swift
// swift-afnetwork
//
// Created by Admin on 2019/2/11.
// Copyright © 2019 Admin. All rights reserved.
//
import UIKit
enumHTTPRequestType {
caseGET
casePOST
}
class AFNetWorkingUtil:AFHTTPSessionManager {
/// 基础url
let BASE_URL = "http://v.juhe.cn"
//单例
staticletshared:AFNetWorkingUtil= {
letinstance =AFNetWorkingUtil()
instance.requestSerializer = AFJSONRequestSerializer()
instance.responseSerializer = AFHTTPResponseSerializer()
instance.requestSerializer.setValue("application/json,text/html", forHTTPHeaderField:"Accept")
instance.requestSerializer.setValue("application/json,charset=utf-8", forHTTPHeaderField: "Content-Type")
returninstance
}()
funcgetBaseUrl(baseUrl:String) ->String{
returnBASE_URL
}
funcrequest(requestType:HTTPRequestType,urlString:String,parameters:[String:AnyObject]?,requestSuccess:@escaping(AnyObject?)->(),requestFailure:@escaping(_error:Error)->()){
let success = {
(tasks:URLSessionDataTask,json:Any) ->()in requestSuccess(json as AnyObject?)
}
let failure = {
(tasks:URLSessionDataTask?,error:Error) ->()in requestFailure(error)
}
if requestType == .GET{
get(getBaseUrl(baseUrl:BASE_URL)+urlString,parameters: parameters,progress:nil, success: success, failure: failure)
}else{
self.post(urlString, parameters: parameters, progress:nil, success: success, failure: failure)
}
}
}
网友评论