提问者:小点点

选择基于o的计算,其中textfield留空


我正在尝试制作一个使用3个变量的代码(在Swift 4 xcode 9中),其中2个变量由用户通过TextFields给出。另一个变量的值将根据哪个变量留空来计算。例如,如果用户将textfield1留空,则当按下按钮时,textfield2和textfield3的输入将用于计算。如果用户将textfield2留空,则使用textfield1和textfield3的输入,以此类推...我还设置了委托,将UITextField转换为float等。

我正在尝试创建一个选择器,在该选择器的基础上textfield留空,但没有成功...

提前道谢!

//instance variables to hold values entered by the user
var one : Float = 0
var two : Float = 0
var three : Float = 0

//TextFields variables
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField3: UITextField!

func calculate() {
    var result : Float = 0

    // *** Selector ***

    //if user leaves textfield1 blank
    if (textField1.text?.isEqual(nil))! {
        result = textField2 * textfield3
        resultLabel.text = "\(result)"
        }

    //if user leaves textField2 blank
    else if (textField2.text?.isEqual(nil))! {
        result = textfield1 / textField3
        resultLabel.text = "\(result)"
            }

    //if user leaves textField3 blank
    else if (textfield3.text?.isEqual(nil))! {

        result = textfield1 + textfield2
        resultLabel.text = "\(result)"
            }
    }

共2个答案

匿名用户

你正在尝试做的是相当手动的,不是推荐的方式。Swift支持很多转换函数来完成这样的工作。

首先创建一个数组来存储文本字段:

var inputs: [UITextField] = [UITextField]()

override func viewDidLoad() {
    super.viewDidLoad()

    inputs.append(textField1)
    inputs.append(textField2)
    inputs.append(textField3)
}

现在您有了一个包含所有输入的数组。我们现在的工作是转换输入并得到结果值:

func calculation() {
    var blankIndex = 0 // keep track of which input is blank

    // let transform our inputs into float values
    let values = inputs.enumerated()
        .map { (i, textField) -> Float? in
            if let text = textField.text, !text.isEmpty {
                blankIndex = i // we know which input is blank
                return Float(text)!
            }
            return nil
        }
        .filter { $0 != nil }
        .map { $0! }

    // now we need to validate out inputs
    // only one input is left blank by the user
    if values.count != 2 {
        // show alert for notifying user about this

        // stop our calculation
        return
    }

    // now our values contain only valid inputs the we need,
    // we can start do our calculation now
    let result: Float
    switch blankIndex {
    case 0: result = values[0] * values[1]
    case 1: result = values[0] / values[1]
    default: result = values[0] + values[1]
    }
}

此时,result变量是您需要的结果

匿名用户

您试图对uitextfield执行数学运算,这显然是不可能的。文本字段包含字符串。您希望对这些字符串表示的float进行数学运算。

如果您将您的问题概括一下,它基本上可以归结为:

  • 找出三个字段中的哪个字段为空(只允许有1个字段为空)
  • 将其他2个字段的字符串值转换为floatS
  • 对这2float执行数学运算,并将结果输出到标签

代码:

func calculate() {
    // These are the possible operations that you will perform
    // Each is a closure that takes 2 Floats and return a Float
    let plus     = { (lhs: Float, rhs: Float) in lhs + rhs }
    let multiply = { (lhs: Float, rhs: Float) in lhs * rhs }
    let divide   = { (lhs: Float, rhs: Float) in lhs / rhs }

    var lhsStr = ""         // the string stored in the first non-empty text field
    var rhsStr = ""         // the string stored in the second non-empty text field
    var operation = plus    // don't worry about the default. We will change this later

    // Find which field is empty
    // We allow only one of the fields to be empty, not 0, 2, or 3.
    switch (textField1.text!.isEmpty, textField2.text!.isEmpty, textField3.text!.isEmpty) {
    case (true, false, false):
        lhsStr = textField2.text!
        rhsStr = textField3.text!
        operation = multiply
    case (false, true, false):
        lhsStr = textField1.text!
        rhsStr = textField3.text!
        operation = divide
    case (false, false, true):
        lhsStr = textField1.text!
        rhsStr = textField2.text!
        operation = plus
    default:
        fatalError("One and only one of the fields must be empty")
    }

    // Now convert the 2 Strings to Floats
    if let lhs = Float(lhsStr), let rhs = Float(rhsStr) {
        let result = operation(lhs, rhs)
        resultLabel.text = "\(result)"
    } else {
        fatalError("Cannot convert string to number")
    }
}