MG--Swift面向协议开发
Mg明明就是你 2021/1/13 13:07:21 次
得益于面向对象语言的特性 (封装、继承、多态) 在我们熟悉的设计模式中渐渐形成统一的软件开发思想,但是由于OC的局限性, 使得iOS开发组件化编程变得十分繁琐,需要将一些功能拆分为类,在抽取某些功能作为基类的不断运用中,代码的可移植性逐渐减弱。就如同一棵树,从主干到各个分支,每个分支再长成细枝末叶。代码的耦合性也相应增加。随着苹果 swift 语言的推出,对于传统OC 语言取其精华,弃其糟粕。 加上开源社区众大神的齐力维护。逐渐成为一门非常优秀的高级语言。其中Swift中的面向协议编程思想就是其中很有代表性的一项优秀组成部分。
Swift的 POP 是使用了继承的思想,它模拟了多继承关系,实现了代码的跨父类复用,同时也不存在 is-a 关系。swift中主类和 extension扩展类 的协同工作,保留了 在主类中定义方法 在所继承的类中进行实现的特性,又新增了在 extension拓展类 中定义并实现的方法在任何继承自此协议的类中可以任意调用,从而实现组件化编程。
自定义协议
- 于是我们可以使用协议做一些优化,精简开发中所写代码,👇请看代码
// // MGProtocol+extesnsion.swift // MGSDK // // Created by Nicky Y M LIU_SP on 2021/1/11. // Copyright © 2021 Nicky Y M LIU_SP. All rights reserved. // import UIKit public protocol MGReusableViewAble: class {} extension MGReusableViewAble where Self: UIView { public static var reuseIdentifier: String { return String("\(self)") } } public protocol MGNibLoadAbleViewAble: class { } extension MGNibLoadAbleViewAble where Self: UIView { public static func loadViewWithNib() -> Self { return Bundle.main.loadNibNamed("\(self)", owner: nil, options: nil)?.last! as! Self } public static var nibName: String { return String("\(self)") } } // MARK: - extension extension UITableView { // Register public func registerCellFromNib<T: UITableViewCell>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble { let Nib = UINib(nibName: T.nibName, bundle: nil) register(Nib, forCellReuseIdentifier: T.reuseIdentifier) } public func registerCell<T: UITableViewCell>(_: T.Type) where T: MGReusableViewAble { register(T.self, forCellReuseIdentifier: T.reuseIdentifier) } public func registerHeaderFooterFromNib<T: UITableViewHeaderFooterView>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble { let Nib = UINib(nibName: T.nibName, bundle: nil) register(Nib, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) } public func registerHeaderFooter<T: UITableViewHeaderFooterView>(_: T.Type) where T: MGReusableViewAble { register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier) } public func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: MGReusableViewAble { guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else { fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)") } return cell } } extension UICollectionView { // Register public func registerCellFromNib<T: UICollectionViewCell>(_: T.Type) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble { let Nib = UINib(nibName: T.nibName, bundle: nil) register(Nib, forCellWithReuseIdentifier: T.reuseIdentifier) } public func registerCell<T: UICollectionViewCell>(_: T.Type) where T: MGReusableViewAble { register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier) } // elementKindSectionHeader elementKindSectionFooter public func registerHeaderFooter<T: UICollectionReusableView>(_: T.Type, elementKind: String) where T: MGReusableViewAble, T: MGNibLoadAbleViewAble { let Nib = UINib(nibName: T.nibName, bundle: nil) register(Nib, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier) } public func registerHeaderFooter<T: UICollectionReusableView>(_: T.Type, elementKind: String) where T: MGReusableViewAble { register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier) } // dequeueReusable public func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: MGReusableViewAble { guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else { fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)") } return cell } }
使用
- 我们如何使用呢,只需要创建的class或者struct 遵守协议,便拥有了此方法,
- 举个例子来说,.xib 加载 UIView,只要在xib加载的类中继承此协议MGNibLoadAbleViewAble\color{#FF0000}{MGNibLoadAbleViewAble}MGNibLoadAbleViewAble,
class MGRedView: UIView, MGNibLoadAbleViewAble {}
,就可以在需要初始化此对象时直接调用let view = MGRedView.loadViewWithNib()
,也可以查看下面的代码
下面我们来精简UITableView的注册和循环利用方法
- MGTableViewCell遵守MGReusableViewAble\color{#0000FF}{MGReusableViewAble}MGReusableViewAble协议
import UIKit import MGSDK class MGTableViewCell: UITableViewCell, MGReusableViewAble { override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
- 主要看registerCell\color{#0000FF}{registerCell}registerCell和 dequeueReusableCell\color{#0000FF}{dequeueReusableCell}dequeueReusableCell方法
import UIKit import MGSDK class MGTabViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .white self.view.addSubview(self.tableView) tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor,constant: 0).isActive = true tableView.topAnchor.constraint(equalTo: self.view.topAnchor,constant: 0).isActive = true tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor,constant: 0).isActive = true tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor,constant: 0).isActive = true } private lazy var tableView: UITableView = { let tableView = UITableView(frame: self.view.frame, style: UITableView.Style.plain) tableView.delegate = self tableView.dataSource = self tableView.tableFooterView = UIView() tableView.registerCell(MGTableViewCell.self) return tableView }() } extension MGTabViewController :UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 10 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as MGTableViewCell return cell } }

关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[MG--Swift面向协议开发]http://www.zyiz.net/tech/detail-150877.html
您可能感兴趣的文章:
- 2019-07-09Swift和C语言混合编程教程
- 2021-01-27MG--Swift遮照HUD 1
- 2021-01-27MG--Swift遮照HUD 2
- 2021-01-25Objective-C 之父 Brad J. Cox 去世,他推动了苹果软件生态的发展
- 2021-01-21间谍软件盯上 iPhone 用户,数月内或遭受大规模攻击
- 2021-01-153倍+提升,高德地图极致性能优化之路
- 2020-12-02RxSwift系列—RxSwift核心逻辑
- 2020-12-02RxSwift系列—基本概念
- 2020-12-02RxSwift系列—RxSwift高阶函数
- 2020-11-21Google 2020开发者大会Flutter专题

扫描二维码或查找【程序员编程王】
可以随时随地学编程啦!
共有 条评论 网友评论