【问题标题】:is passing argument a viewmodel or a protocol正在传递参数视图模型或协议
【发布时间】:2016-06-07 02:58:30
【问题描述】:

代码取自Protocol Oriented MVVM,这就是ViewModel 的样子:

struct MinionModeViewModel: SwitchWithTextCellDataSource {
    var title = "Minion Mode!!!"
    var switchOn = true
}

extension MinionModeViewModel: SwitchWithTextCellDelegate {
    func onSwitchTogleOn(on: Bool) {
        if on {
            print("The Minions are here to stay!")
        } else {
            print("The Minions went out to play!")
        }
    }
    
    var switchColor: UIColor {
        return .yellowColor()
    }
}

我理解这部分。基本上,MinionModeViewModel 覆盖了SwitchWithTextCellDelegate 和SwitchWithTextCellDelegate 的一些默认行为

接下来作者通过传递viewModel 作为参数来配置单元格:

SettingsViewController.swift

let viewModel = MinionModeViewModel()
cell.configure(withDataSource: viewModel, delegate: viewModel)
return cell

然而,在SwitchWithTextTableViewCell,configure 方法的参数是SwitchWithTextCellDataSource 和SwitchWithTextCellDelegate

func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) {
        self.dataSource = dataSource
        self.delegate = delegate
        
        label.text = dataSource.title
        switchToggle.on = dataSource.switchOn
        // color option added!
        switchToggle.onTintColor = delegate?.switchColor
    }

我是 Swift 新手。谁能解释一下

  1. SwitchWithTextTableViewCell 中的configure 方法的名称和含义是什么dataSource 和delegate。他们是External Parameter Names。

  2. 为什么configure 方法的传递参数类型不同:view model vs protocols 类型

【问题讨论】:

    标签: swift struct swift2 swift-protocols


    【解决方案1】:

    首先,MinionModeViewModel 没有覆盖 SwitchWithTextCellDelegate 和 SwitchWithTextCellDelegate 的默认行为。

    术语覆盖与继承一起使用。 示例

    class A {
      func someFunction() {
      }
    }
    class B:A {
      override func someFunction() {
      }
    }
    

    这里的类 B 是类 A 的子类,因此如果类 B 应该提供与其类 A 不同的方法 someFunction 的实现,它应该覆盖它并提供不同的实现。

    协议不同。这是一种抽象类。协议只是一组规则。 因此,当某些类或结构符合协议时,这意味着它们应该实现协议定义中规定的所有必需方法或属性。

    例子:

    protocol SwitchWithTextCellDelegate {
    
      func onSwitchTogleOn(on: Bool)
    
      var switchColor: UIColor {get}
    }
    

    现在任何符合协议 SwitchWithTextCellDelegate 的结构或类都应该实现 onSwitchToggleOn 方法,并且还应该有一个属性 switchColor ,它将有一个 getter。

    就像你所做的那样:

    extension MinionModeViewModel: SwitchWithTextCellDelegate {
        func onSwitchTogleOn(on: Bool) {
        }
    
        var switchColor: UIColor {
            return someColor
        }
    }
    

    方法说明

    1. func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?)
    

    dataSource:在这个参数中我们可以传递任何符合协议 SwitchWithTextCellDataSource 协议的类或结构

    delegate:在这个参数中我们可以传递任何符合SwitchWithTextCellDelegate协议的类或结构体

    现在教程的作者在两个参数中传递了相同的对象可能会让人感到困惑。为什么不将其作为单个参数传递?

    let viewModel = MinionModeViewModel()
    cell.configure(withDataSource: viewModel, delegate: viewModel)
    

    这是因为这里 MinionModeViewModel 符合 SwitchWithTextCellDataSource 和 SwitchWithTextCellDelegate 协议,所以我们可以在两个参数中传递相同的对象。但是这种方法可以灵活地传递任何符合这些协议的对象。

    【讨论】:

      【解决方案2】:

      协议定义了采用对象实现的行为(它们通常不会覆盖行为)。

      在此示例中,SwitchWithTextCellDataSource 和 SwitchWithTextCellDelegates 是 protocols,MinionModelViewModel 继承(采用)。为了更有意义,请考虑您可能有另一个结构:

      struct EvilVillanViewModel: SwitchWithTextCellDataSource {
        var title: "Evil Villian Model!!!"
        var switchOn = false
      }
      

      由于这个结构也采用SwitchWithTextCellDataSource,它可以在任何期望实现struct 的class/struct 的代码中互换 - 因为它知道它将有一个标题和一个 switchOn 值 - 因为协议要求它。没有它,您的代码将无法编译。

      所以....在configure(withDataSource dataSource: SwitchWithTextCellDataSource...) 中,您可以轻松地传入EvilVillanViewModel 结构或MinionModelViewModel,一切都会完美运行。当代码块引用dataSource.title 时,它知道您传递的任何内容都有title,因为它必须符合SwitchWithTextCellDataSource 协议。

      这只是协议力量的开始!希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-19
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多