【问题标题】:Default parameter values error: "instance member cannot be used on type viewcontroller"默认参数值错误:“实例成员不能用于类型视图控制器”
【发布时间】:2015-10-01 09:28:18
【问题描述】:

在我的视图控制器中:

class FoodAddViewController: UIViewController, UIPickerViewDataSource, UITextFieldDelegate, UIPickerViewDelegate {

    let TAG = "FoodAddViewController"

    // Retreive the managedObjectContext from AppDelegate
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    @IBOutlet weak var foodName: UITextField!

    @IBOutlet weak var foodPortion: UITextField!

    @IBOutlet weak var foodCalories: UITextField!

    @IBOutlet weak var foodUnit: UILabel!

    @IBOutlet weak var unitPicker: UIPickerView!

    @IBOutlet weak var unitPickerViewContainer: UIVisualEffectView!

    /*
        unrelated code has been ommited
    */
    func validateAllTextFields(textFields: [UITextField] = [foodName as UITextField, foodPortion, foodCalories]) -> Bool {

        var result = true
        for textField in textFields {
            result = validateTextField(textField) && result
        }
        return result
    }

    func validateTextField(textField: UITextField) -> Bool{
        let correctColor = UIColor.redColor().CGColor, normalColor = UIColor.blackColor().CGColor
        var correct = true

        if textField == foodPortion || textField == foodCalories{
            if !Misc.isInteger(textField.text!){
                correct = false
            }
        }
        if textField.text!.isEmpty {
            correct = false
        }

        textField.layer.borderColor = correct ? normalColor : correctColor

        return correct
    }
}

我有几个文本字段,在我的 validateTextField 中可以一次验证一个,我希望我的 validateAllTextFields 能够通过一一检查来验证给定的文本字段列表,如果没有给出列表,我想要检查包含所有三个文本字段的给定默认列表。

我想象的代码是这样的:

func validateAllTextFields(textFields: [UITextField] = [foodName as UITextField, foodPortion, foodCalories]) -> Bool {

    var result = true
    for textField in textFields {
        result = validateTextField(textField) && result
    }
    return result
}

但是 Xcode 会返回错误:

实例成员不能用于视图控制器类型

是什么原因以及如何解决?

【问题讨论】:

  • 您在哪一行代码中出错了?
  • 方法的第一行。没有 " = [foodName, foodPortion, foodCalories]" 部分,一切正常,

标签: ios uiviewcontroller uitextfield swift2


【解决方案1】:

您不能在函数声明中使用实例变量。使用您的 textFields 数组调用该函数并传递参数。

func validateAllTextFields(textFields: [UITextField] ) -> Bool {

    var result = true
    for textField in textFields {
        result = validateTextField(textField) && result
    }
    return result
}

你们班上有些人:

validateAllTextFields(textFields: [foodName, foodPortion, foodCalories])

或者你检查你的函数内部是否 textFields 是空的,然后你使用实例变量

func validateAllTextFields(textFields: [UITextField] ) -> Bool {
    if textFields.count == 0 {
        textFields = [foodName, foodPortion, foodCalories]
    }
    var result = true
    for textField in textFields {
        result = validateTextField(textField) && result
    }
    return result
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2018-05-08
    相关资源
    最近更新 更多