2016-06-07 2 views
0

코드가 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 인수로 viewModel을 전달하여 몇 가지 기본 SwitchWithTextCellDelegate의 행동과 SwitchWithTextCellDelegate

다음으로 저자는 셀을 구성하는 것입니다을 재정의 :

SettingsViewController.swift 

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

을하지만, SwitchWithTextTableViewCell에서 configure 메소드의 인수 SwitchWithTextCellDataSourceSwitchWithTextCellDelegate 있습니다

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. 누군가가 SwitchWithTextTableViewCellconfigure 방법의

  1. 이름이 무엇인지

    과 그 의미 dataSourcedelegate을 설명해주십시오 수 있습니다. 그들은 External Parameter Names입니까?

  2. configure 방법의 전달 인자의 종류가 다릅니다 : view modelprotocols 대 유형

답변

2

우선 MinionModeViewModel은 SwitchWithTextCellDelegate 및 SwitchWithTextCellDelegate의 기본 동작을 재정의하지 않습니다.

재정의라는 용어는 상속과 함께 사용됩니다. 예

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

클래스 B는 클래스 A의 하위 클래스 및 클래스 B가 클래스 A보다 메소드 된 SomeFuncion의 다른 구현을 제공해야하므로 있으면 그것을 무시하고 다른 구현을 제공한다.

프로토콜이 다릅니다. 그것은 추상적 클래스의 일종입니다. 프로토콜은 규칙 집합입니다. 그래서 일부 클래스 나 구조체가 프로토콜을 따르는 경우 프로토콜 정의에 명시된 모든 필수 메서드 또는 속성을 구현해야한다는 의미입니다.

예 :

이제
protocol SwitchWithTextCellDelegate { 

    func onSwitchTogleOn(on: Bool) 

    var switchColor: UIColor {get} 
} 

게터있을 것이다 속성 switchColor을 가져야한다 또한 방법을 onSwitchToggleOn 구현해야 프로토콜 SwitchWithTextCellDelegate을 준수하고 구조체 또는 클래스를.

당신이했던 것처럼 우리가 프로토콜 SwitchWithTextCellDataSource의 protcol

을 따르는 클래스 나 구조체를 전달할 수 있습니다이 매개 변수에서 :

extension MinionModeViewModel: SwitchWithTextCellDelegate { 
    func onSwitchTogleOn(on: Bool) { 
    } 

    var switchColor: UIColor { 
     return someColor 
    } 
} 

설명 방법

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

은 dataSource에 대한

위임자 예 : :이 매개 변수에서 프로토콜을 준수하는 클래스 또는 구조체를 전달할 수 있습니다. SwitchWithTextCellDelegate

이제 튜토리얼 작성자가 두 매개 변수에서 같은 개체를 전달했는지 혼동을 일으킬 수 있습니다. 왜 그것을 하나의 매개 변수로 전달하지 않습니까?

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

MinionModeViewModel 그래서 우리는 두 매개 변수에 동일한 개체를 전달할 수 SwitchWithTextCellDataSource 및 SwitchWithTextCellDelegate 프로토콜 모두에 부합 여기에 때문입니다. 그러나이 방법은 이러한 프로토콜을 준수하는 모든 객체를 전달할 수있는 유연성을 제공합니다.

1

프로토콜이 채택 개체 (그들은 행동을 덮어되지 않습니다 - 보통) 구현하는 것이 동작을 정의합니다.

이 예에서는 MinionModelViewModel이 계승 (채택) 한 SwitchWithTextCellDataSourceSwitchWithTextCellDelegatesprotocols입니다. 그것은 것을 알고 있기 때문에 - 그것은 어디 코드 SwitchWithTextCellDataSource 구현하는 class/struct 기대된다 교환 할,이 또한 구조체 SwitchWithTextCellDataSource을 채택하기 때문에

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

이 더 이해하려면, 다른 구조체가있을 수 있습니다 고려 제목이 있고 switchOn 값 - 프로토콜이 요구하기 때문에. 코드가 없으면 코드가 컴파일되지 않습니다.

이렇게 .... configure(withDataSource dataSource: SwitchWithTextCellDataSource...)에서 EvilVillanViewModel 구조체 또는 MinionModelViewModel을 쉽게 전달할 수 있으며 모든 것이 완벽하게 작동합니다. 코드 블록이 dataSource.title을 가리킬 때 titleSwitchWithTextCellDataSource 프로토콜을 준수해야하므로 코드 블록이 dataSource.title을 참조하면 전달한 값이 title임을 알 수 있습니다.

그리고 이것은 프로토콜의 힘의 시작에 불과합니다. 희망이 도움이!