本文由CocoaChina译者lynulzy(社区ID,博客)翻译
原文:Swift 2.0: Protocol-Oriented MVVM
自从令人兴奋的[
《面向协议的编程方法》
]在Swift的WWDC大会上发布以来。我对协议的使用考虑了很多。但是在现实中,我并没有太多的顾及和使用这些功能。我还仍旧在消化到底面向协议的编程方法是什么,在代码的哪些地方应该使用,而不是使用我目前使用的`go-to`编程方法。
...所以,当我想起来要在哪里应用这些概念性的东西时,我非常激动,那就是MVVM !我已经在之前的博客中使用过MVVM架构,如果你想了解更多MVVM相关知识请参考[这里]。接下来我将讲解,如何添加面向协议。
我将会使用一个简单的例子。一个只有一个设置选项的设置页面,把应用设置为Minion模式,当然你也可以扩展为多个设置选项。
View Cell
一个极其普通的Cell,它包含一个Label和一个开关控件。你也可以在其他地方使用这个Cell,例如注册页面添加一个“记住我”的开关选项。所以,你应该保持这个页面通用性。
一个复杂的配置
通常,我在cell中使用一个设置方法,来监听所有对应用设置可能的变更,这看起来是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
classSwitchWithTextTableViewCell:UITableViewCell{
@IBOutletprivateweak
var
label:UILabel!
switchToggle:UISwitch!
typealiasonSwitchToggleHandlerType=(switchOn:Bool)->Void
private
onSwitchToggleHandler:onSwitchToggleHandlerType?
overridefuncawakeFromNib(){
super
.awakeFromNib()
}
funcconfigure(withTitletitle:String,
switchOn:Bool,
onSwitchToggleHandler:onSwitchToggleHandlerType?=nil)
{
label.text=title
switchToggle.on=switchOn
self.onSwitchToggleHandler=onSwitchToggleHandler
}
@IBActionfunconSwitchToggle(sender:UISwitch){
onSwitchToggleHandler?(switchOn:sender.on)
}
}
|
通过 Swift 的默认参数,可以添加其他的设置选项到这个设置方法,而不必改变代码中的其他地方,使用起来非常方便。例如,当设计师说开关按钮的颜色需应该各不相同,这时候我就可以添加一个默认参数。
12
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
switchColor:UIColor=.purpleColor(),
onSwitchToggleHandler:onSwitchToggleHandlerType?=nil)
{
label.text=title
switchToggle.on=switchOn
switchToggle.onTintColor=switchColor
self.onSwitchToggleHandler=onSwitchToggleHandler
虽然在这种情况下看起来并不是什么大问题,但是随着时间的增加,事实上这个方法将会变得非常冗长、复杂!是时候由面向协议的编程方法登场了。
面向协议的编程方法
26
27
28
29
protocolSwitchWithTextCellProtocol{
title:String{get}
switchOn:Bool{get}
funconSwitchTogleOn(on:Bool)
}
classSwitchWithTextTableViewCell:UITableViewCell{
label:UILabel!
switchToggle:UISwitch!
delegate:SwitchWithTextCellProtocol?
overridefuncawakeFromNib(){
.awakeFromNib()
}
funcconfigure(withDelegatedelegate:SwitchWithTextCellProtocol){
self.delegate=delegate
label.text=delegate.title
switchToggle.on=delegate.switchOn
}
@IBActionfunconSwitchToggle(sender:UISwitch){
delegate?.onSwitchTogleOn(sender.on)
}
当设计师说需要改变开关控件颜色的时候会发生什么?以下代码可以展现协议扩展的奇妙之处。
21
extensionSwitchWithTextCellProtocol{
//setthedefaultcolorhere!
funcswitchColor()->UIColor{
return
.purpleColor()
}
}
classSwitchWithTextTableViewCell:UITableViewCell{
//truncated,seeabove
funcconfigure(withDelegatedelegate:SwitchWithTextCellProtocol){
self.delegate=delegate
label.text=delegate.title
switchToggle.on=delegate.switchOn
//coloroptionadded!
switchToggle.onTintColor=delegate.switchColor()
}
在以上代码中协议的扩展实现了默认的switchColor选项,所以,任何已经实现了这个协议或者并不关心设置开关颜色的人,不用关注这个扩展。只有一个具有不同颜色的新的开关控件可以实现。
viewmodel
所以现在剩下的事情将会非常简单。我将会为MinionMode的设置cell写一个viewmodel。
18
importUIKit
structMinionModeviewmodel:SwitchWithTextCellProtocol{
title=
"MinionMode!!!"
switchOn=
true
funconSwitchTogleOn(on:Bool){
if
on{
print(
"TheMinionsareheretostay!"
)
}
else
{
"TheMinionswentouttoplay!"
)
}
}
funcswitchColor()->UIColor{
.yellowColor()
}
ViewController
最后一步就是在ViewController中设置cell的时候将viewmodel传给cell。
29
30
31
32
33
34
35
36
37
38
39
classSettingsViewController:UITableViewController{
enumSetting:Int{
case
MinionMode
//othersettingshere
}
overridefuncviewDidLoad(){
.viewDidLoad()
//MARK:-Tableviewdatasource
overridefunctableView(tableView:UITableView,
numberOfRowsInSectionsection:Int)->Int
{
1
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell
{
letsetting=Setting(rawValue:indexPath.row){
switch
setting{
.MinionMode:
letcell=tableView.dequeueReusableCellWithIdentifier(
"SwitchWithTextTableViewCell"
,forIndexPath:indexPath)as!SwitchWithTextTableViewCell
//thisiswherethemagichappens!
cell.configure(withDelegate:MinionModeviewmodel())
cell
}
}
tableView.dequeueReusableCellWithIdentifier(
"defaultCell"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)
}
通过使用协议的扩展,是面向协议的编程方法有了很大的意义,并且我在寻找更多的使用场景。以上代码的全部内容放在[github]上。
更新:将数据源和代理分开
在评论中,marc Baldwin 建议分开cell的数据源和代理方法到两个协议中,就像UITableView中的那样。我很赞成这个意见,以下是我修改后的代码。
View Cell
Cell将拥有两个协议,并且任何一个协议都可以设置这个cell。
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
protocolSwitchWithTextCellDataSource{
title:String{get}
switchOn:Bool{get}
protocolSwitchWithTextCellDelegate{
funconSwitchTogleOn(on:Bool)
switchColor:UIColor{get}
textColor:UIColor{get}
font:UIFont{get}
}
extensionSwitchWithTextCellDelegate{
switchColor:UIColor{
.purpleColor()
textColor:UIColor{
.blackColor()
font:UIFont{
.systemFontOfSize(17)
}
classSwitchWithTextTableViewCell:UITableViewCell{
label:UILabel!
switchToggle:UISwitch!
dataSource:SwitchWithTextCellDataSource?
delegate:SwitchWithTextCellDelegate?
overridefuncawakeFromNib(){
.awakeFromNib()
}
funcconfigure(withDataSourcedataSource:SwitchWithTextCellDataSource,delegate:SwitchWithTextCellDelegate?){
self.dataSource=dataSource
self.delegate=delegate
label.text=dataSource.title
switchToggle.on=dataSource.switchOn
//coloroptionadded!
switchToggle.onTintColor=delegate?.switchColor
}
@IBActionfunconSwitchToggle(sender:UISwitch){
delegate?.onSwitchTogleOn(sender.on)
}
viewmodel
你现在可以在扩展里把数据源和delegate逻辑分开了:
structMinionModeviewmodel:SwitchWithTextCellDataSource{
true
extensionMinionModeviewmodel:SwitchWithTextCellDelegate{
funconSwitchTogleOn(on:Bool){
on{
)
{
)
}
}
.yellowColor()
ViewController
这一部分是我不十分确定,ViewController不能传递viewmodel两次:
17
cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell
{
letsetting=Setting(rawValue:indexPath.row){
setting{
.MinionMode:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)as!SwitchWithTextTableViewCell
//thisiswherethemagichappens!
letviewmodel=MinionModeviewmodel()
cell.configure(withDataSource:viewmodel,delegate:viewmodel)
cell
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forIndexPath:indexPath)
代码已经上传[GitHub]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。
相关推荐
效率成吨提升之代码生成器-蓝湖工具神器iOS,Android,Swift,Flutter
软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘贴.待开发的功能:1.支持自动生成约束2.开发设置页面3.做一个浏览器插件,支持不需要下载整个工程,可即时操作当前蓝湖浏览页面4.支持Flutter语言模板生成5.支持更多平台,如Sketch等6.支持用户自定义语言模板
【Audio音频开发】音频基础知识及PCM技术详解
现实生活中,我们听到的声音都是时间连续的,我们称为这种信号叫模拟信号。模拟信号需要进行数字化以后才能在计算机中使用。目前我们在计算机上进行音频播放都需要依赖于音频文件。那么音频文件如何生成的呢?音频文件的生成过程是将声音信息采样、量化和编码产生的数字信号的过程,我们人耳所能听到的声音频率范围为(20Hz~20KHz),因此音频文件格式的最大带宽是20KHZ。根据奈奎斯特的理论,音频文件的采样率一般在40~50KHZ之间。奈奎斯特采样定律,又称香农采样定律。...............
见过仙女蹦迪吗?一起用python做个小仙女代码蹦迪视频
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿遍又亿遍,久久不能离开!看着小仙紫姐姐的蹦迪视频,除了一键三连还能做什么?突发奇想,能不能把舞蹈视频转成代码舞呢?说干就干,今天就手把手教大家如何把跳舞视频转成代码舞,跟着仙女姐姐一起蹦起来~视频来源:【紫颜】见过仙女蹦迪吗 【千盏】一、核心功能设计总体来说,我们需要分为以下几步完成:从B站上把小姐姐的视频下载下来对视频进行截取GIF,把截取的GIF通过ASCII Animator进行ASCII字符转换把转换的字符gif根据每
自定义ava数据集及训练与测试 完整版 时空动作/行为 视频数据集制作 yolov5, deep sort, VIA MMAction, SlowFast
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至2022年4月底。我已经将这篇博客的内容写为论文,上传至arxiv:https://arxiv.org/pdf/2204.10160.pdf欢迎大家指出我论文中的问题,特别是语法与用词问题在github上,我也上传了完整的项目:https://github.com/Whiffe/Custom-ava-dataset_Custom-Spatio-Temporally-Action-Video-Dataset关于自定义ava数据集,也是后台
【视频+源码】登录鉴权的三种方式:token、jwt、session实战分享
因为我既对接过session、cookie,也对接过JWT,今年因为工作需要也对接了gtoken的2个版本,对这方面的理解还算深入。尤其是看到官方文档评论区又小伙伴表示看不懂,所以做了这期视频内容出来:视频在这里:本期内容对应B站的开源视频因为涉及的知识点比较多,视频内容比较长。如果你觉得看视频浪费时间,可以直接阅读源码:goframe v2版本集成gtokengoframe v1版本集成gtokengoframe v2版本集成jwtgoframe v2版本session登录官方调用示例文档jwt和sess
【Android App】实战项目之仿微信的私信和群聊App附源码和演示视频 超详细必看
【Android App】实战项目之仿微信的私信和群聊App(附源码和演示视频 超详细必看)
采用MATLAB对正弦信号,语音信号进行生成、采样和恢复,利用MATLAB工具箱对混杂噪声的音频信号进行滤波
采用MATLAB对正弦信号,语音信号进行生成、采样和内插恢复,利用MATLAB工具箱对混杂噪声的音频信号进行滤波
Keras深度学习实战40——音频生成
随着移动互联网、云端存储等技术的快速发展,包含丰富信息的音频数据呈现几何级速率增长。这些海量数据在为人工分析带来困难的同时,也为音频认知、创新学习研究提供了数据基础。在本节中,我们通过构建生成模型来生成音频序列文件,从而进一步加深对序列数据处理问题的了解。
- • 效率成吨提升之代码生成器-蓝湖工具神器…
- • 【Audio音频开发】音频基础知识及PCM技…
- • 见过仙女蹦迪吗?一起用python做个小仙…
- • 【Android App】实战项目之仿抖音的短视…
- • 自定义ava数据集及训练与测试 完整版 时…
- • 【视频+源码】登录鉴权的三种方式:tok…
- • 【Android App】实战项目之仿微信的私信…
- • 零基础用Android Studio实现简单的本地…
- • 采用MATLAB对正弦信号,语音信号进行生…
- • Keras深度学习实战40——音频生成
- • 视频实时行为检测——基于yolov5+deeps…
- • 数电实验 数字电子钟设计 基于quartus …
- • 腾讯会议使用OBS虚拟摄像头
- • 文本生成视频Make-A-Video,根据一句话…
- • 信号处理——MATLAB音频信号加噪、滤波
- • 【新知实验室 - TRTC 实践】音视频互动…
- • Keras深度学习实战39——音乐音频分类
- • C++游戏game | 井字棋游戏坤坤版配资源…
|
|
|
|
|
|
|