2016-11-20 2 views
-1

私はSwift 2.3を使用しており、UIViewControllerが更新されたビューモデルで自身を更新するコールバック関数を作成しようとしていますが、ビューモデルクラスでコンパイルエラーが発生しています。他のエラーも出ていますが、それらはすべて私のCalculatorViewModelクラスの根本的な問題の原因と思われます。この偉大なpostで使用されているiOSアーキテクチャのパターンについて、MVVMの例に従っており、これを自分のアプリに適用しようとしていることに注目しておきましょう。「ここで入力できません」とはどういう意味ですか?

ここに私のビューコントローラです:

protocol CalculatorViewModelProtocol: class { 
    var liftName: String? { get } 
    var weightLifted: Double? { get } 
    var repetitions: Int? { get } 
    var oneRepMax: String? { get set } 
    var oneRepMaxDidChange: ((CalculatorViewModelProtocol) ->())? { get set } 
    var units: String? { get } 
    var date: String? { get } 
    func calculateOneRepMax() 

**// the 'Type not allowed here' error is here** 
class CalculatorViewViewModel: CalculatorViewModelProtocol, LiftEventDataManagerDelegate {  

    let calculator = CalculatorBrain() 
    private let dataManager = LiftEventDataManager() 

    var liftName: String? 
    var weightLifted: String! 
    var repetitions: String! 
    var oneRepMax: String? { 
     didSet { 
      self.oneRepMaxDidChange?(self) 
     } 
    } 
    var units: String? 
    var date: String? 

    var oneRepMaxDidChange: ((CalculatorViewModelProtocol) ->())? 

    @objc func calculateOneRepMax(weightLifted: String, repetitions: String) { 
     let result = calculator.calculateOneRepMax(Double(weightLifted)!, repetitions: UInt8(repetitions)!) 
    } 

init() { 

    dataManager.requestData(withViewModel: self) 
    } 
} 

私は多くのことをやった:

class CalculatorViewController: UIViewController, UITextFieldDelegate, DismissalDelegate { 


    var viewModel: CalculatorViewModelProtocol! { 
    didSet { 
     self.viewModel.oneRepMaxDidChange = { [unowned self] viewModel in 
     self.oneRepMaxField.text = String(viewModel.oneRepMax!) 
     } 
    } 
    } 

override func viewDidLoad() { 

super.viewDidLoad() 

let viewModel = CalculatorViewModel() // use of unresolved identifier 'CalculatarViewModel' 

self.viewModel = viewModel 

liftNameButton.setTitle(viewModel.liftName, forState: .Normal) 
weightLiftedField.text = String(viewModel.weightLifted) 
repetitionsField.text = String(viewModel.repetitions) 
units.text = viewModel.units 
oneRepMaxField.text = String(viewModel.oneRepMax!) 

// a bunch of formatting code and then I add a target to a button the user will press: 

calculateButton.addTarget(self, action: #selector(onCalculateTapped), forControlEvents: UIControlEvents.TouchUpInside) 

func onCalculateButtonTapped() { 
if let weightLifted = weightLiftedField.text, let repetitions = repetitionsField.text { 

// error: Argument passed to call that takes no arguments (except that it does) 
viewModel!.calculateOneRepMax(weightLifted, repetitions: repetitions) 
    //weightPercentages = getPercentages(pureOneRepMax!) 
} else { 
    return 
} 

、ここでは、私の見解モデルと「入力許可されていないエラーが」見えるビューモデルプロトコルです検索しても助けになる答えは見つかりませんでした。

+1

プロトコル内でクラス(CalculatorViewViewModel)を宣言しようとしています。それをしないでください。 – rmaddy

+0

あなたのコードは奇妙です。私がそれを合成するなら、 'プロトコルP:クラス{クラスA}'それはどういう意味ですか?私はあなたが ":class"を削除し、あなたの "class Calculator ..."の前にあなたのブレースを閉じなければならないと思います。 –

+0

あなたのコメントにより、私はそのプロトコル定義にもっと集中しました。すべてがアップです。私はそれを修正しましたが、今はクラスがプロトコルに準拠していないと不平を言っていますが、私はそれに対処する方法を知っていると思います。みんなありがとう。 – Jim

答えて

1

プロトコル内でクラスを実装することはできません。 CalculatorViewModelを別のファイルに移動するか、少なくともプロトコルの範囲外にする

関連する問題