“此处不允许输入”是什么意思?

时间:2016-11-20 01:45:13

标签: swift mvvm callback protocols

我正在使用Swift 2.3并尝试创建一个回调函数,这将导致我的UIViewController使用更新的视图模型更新自己,但是我的视图模型类中出现了编译错误 - '允许在这里'。我也遇到了其他错误,但它们似乎都是由CalculatorViewModel类的基本问题引起的。值得注意的是,我正在关注这个关于iOS体系结构模式的post这个伟大的http://localhost:8001/admin/中使用的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
}

这是我的视图模型和视图模型协议,其中出现'Type not allowed error':

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 个答案:

答案 0 :(得分:1)

您无法在协议中实现类。将您的CalculatorViewModel移动到单独的文件或至少在协议范围之外