默认参数值错误:“不能在类型viewcontroller上使用实例成员". [英] Default parameter values error: "instance member cannot be used on type viewcontroller"

查看:124
本文介绍了默认参数值错误:“不能在类型viewcontroller上使用实例成员".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图控制器中:

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能够通过逐个检查它们来验证文本字段的给定列表,如果未给出列表,我想检查包含所有三个文本字段的给定默认列表.

I have a few textfields, and in my validateTextField can verify one at a time, and I want my validateAllTextFields be able to verify a give list of textfield by checking them one by one, if the list is not given, I want to check a given default list that contains all three textfield.

我想象的代码是这样的:

The code I imagine to be something like:

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

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

但是Xcode会返回错误:

However Xcode gives an error back:

实例成员不能在类型viewcontroller上使用

instance member cannot be used on type viewcontroller

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

What's the cause and how to fix?

推荐答案

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

You cannot use instance variables in function declarations. Call the function with your textFields array and pass the parameters.

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

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

有人在您的班上:

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

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

Or you check inside of your function if textFields is empty and than u use the instance variables

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
}

这篇关于默认参数值错误:“不能在类型viewcontroller上使用实例成员".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆