2016-11-20 60 views
-1

我正在使用Swift 2.3並試圖創建一個回調函數,這將導致我的UIViewController使用更新的視圖模型進行自我更新,但在視圖模型類中出現編譯錯誤 - '此處不允許輸入'。我也遇到了其他錯誤,但他們都似乎是由我的CalculatorViewModel類的基本問題造成的。值得注意的是,我正在關注這個偉大的post中關於iOS架構模式的MVVM示例,並試圖將其應用於我的應用。'此處不允許輸入'是什麼意思?

這裏是我的視圖控制器:

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 
} 

,這裏是我的視圖模型,並在「鍵入不允許錯誤」出現一個視圖模型協議:

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) 
    } 
} 

我已經做了很多的搜索,但沒有找到任何幫助的答案。

+1

您試圖在協議內聲明一個類('CalculatorViewViewModel')。不要這樣做。 – rmaddy

+0

你的代碼很奇怪。如果我綜合它:'協議P:class {class A}'它應該是什麼意思?我認爲你應該刪除「:class」,並在你的「class Calculator ...」之前關閉你的大括號。 –

+0

你的評論讓我更關注該協議定義,事實證明我不小心刪除了關閉的'}'一切都起來。我解決了這個問題,現在我抱怨說我的課程不符合協議,但我想我知道如何處理這個問題。多謝你們。 – Jim

回答

1

你不能在協議內實現一個類。將您的CalculatorViewModel移動到單獨的文件中,或者至少在協議範圍之外