swift3.0 面向协议开发 MVP 来看看吧

前端之家收集整理的这篇文章主要介绍了swift3.0 面向协议开发 MVP 来看看吧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

贡献者:赵大财
博客:https://my.oschina.net/zhaodacaiGitHub:https://github.com/dacaizhao
邮箱: dacai_zhao@163.com QQ:327532817
=============================
下载地址:https://github.com/dacaizhao/swiftMVP 喜欢就给我star吧
先看:ViewController

class ViewController: UIViewController {
    
    fileprivate lazy var presenter : ViewPresenster = {
        return ViewPresenster(presenter: self)
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
     }
    
    override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
        presenter.getData()
    }
}

// MARK:- 获取数据//
extension ViewController:ViewPresensterProtocol{
    func showPost(_ resulet: [DCModel]) {
        print(resulet)
    }
}

在看 Model

class DCModel: NSObject {
    
    // 用户ID
    var user_id : String = ""
    
    // 用户名字
    var user_name : String = ""
    
    init(dict : [String : Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?,forUndefinedKey key: String) {}
}


最后 Presenter

protocol ViewPresensterProtocol {
    func showPost(_ resulet: [DCModel])
}

class ViewPresenster: NSObject {
    var presenter: ViewPresensterProtocol!
    lazy var model:[DCModel] = [DCModel]()
    init(presenter:ViewPresensterProtocol) {
        self.presenter = presenter;
    }
    
    func getData(){
        let dict = [
            ["user_id":"1","user_name":"zhaodacai1"],["user_id":"2","user_name":"zhaodacai2"],["user_id":"3","user_name":"zhaodacai3"],["user_id":"4","user_name":"zhaodacai4"],["user_id":"5","user_name":"zhaodacai5"],["user_id":"6","user_name":"zhaodacai6"],["user_id":"7","user_name":"zhaodacai7"]
        ]
        
        for item in dict {
            model.append(DCModel(dict: item))
        }
        
        self.presenter.showPost(model)
    }
    
    
}

猜你在找的Swift相关文章